<li><a href="#When-was-flex-born_003f">When was flex born?</a>
<li><a href="#How-do-I-expand-_005c-escape-sequences-in-C_002dstyle-quoted-strings_003f">How do I expand \ escape sequences in C-style quoted strings?</a>
<li><a href="#Why-do-flex-scanners-call-fileno-if-it-is-not-ANSI-compatible_003f">Why do flex scanners call fileno if it is not ANSI compatible?</a>
<li><a href="#Does-flex-support-recursive-pattern-definitions_003f">Does flex support recursive pattern definitions?</a>
<li><a href="#How-do-I-skip-huge-chunks-of-input-_0028tens-of-megabytes_0029-while-using-flex_003f">How do I skip huge chunks of input (tens of megabytes) while using flex?</a>
<li><a href="#Flex-is-not-matching-my-patterns-in-the-same-order-that-I-defined-them_002e">Flex is not matching my patterns in the same order that I defined them.</a>
<li><a href="#My-actions-are-executing-out-of-order-or-sometimes-not-at-all_002e">My actions are executing out of order or sometimes not at all.</a>
<li><a href="#How-can-I-have-multiple-input-sources-feed-into-the-same-scanner-at-the-same-time_003f">How can I have multiple input sources feed into the same scanner at the same time?</a>
<li><a href="#Can-I-build-nested-parsers-that-work-with-the-same-input-file_003f">Can I build nested parsers that work with the same input file?</a>
<li><a href="#How-can-I-match-text-only-at-the-end-of-a-file_003f">How can I match text only at the end of a file?</a>
<li><a href="#How-can-I-make-REJECT-cascade-across-start-condition-boundaries_003f">How can I make REJECT cascade across start condition boundaries?</a>
<li><a href="#Why-cant-I-use-fast-or-full-tables-with-interactive-mode_003f">Why can't I use fast or full tables with interactive mode?</a>
<li><a href="#How-much-faster-is-_002dF-or-_002df-than-_002dC_003f">How much faster is -F or -f than -C?</a>
<li><a href="#If-I-have-a-simple-grammar-cant-I-just-parse-it-with-flex_003f">If I have a simple grammar can't I just parse it with flex?</a>
<li><a href="#Why-doesnt-yyrestart_0028_0029-set-the-start-state-back-to-INITIAL_003f">Why doesn't yyrestart() set the start state back to INITIAL?</a>
<li><a href="#How-can-I-match-C_002dstyle-comments_003f">How can I match C-style comments?</a>
<li><a href="#The-period-isnt-working-the-way-I-expected_002e">The '.' isn't working the way I expected.</a>
<li><a href="#Can-I-get-the-flex-manual-in-another-format_003f">Can I get the flex manual in another format?</a>
<li><a href="#Does-there-exist-a-_0022faster_0022-NDFA_002d_003eDFA-algorithm_003f">Does there exist a "faster" NDFA->DFA algorithm?</a>
<li><a href="#How-does-flex-compile-the-DFA-so-quickly_003f">How does flex compile the DFA so quickly?</a>
<li><a href="#How-can-I-use-more-than-8192-rules_003f">How can I use more than 8192 rules?</a>
<li><a href="#How-do-I-abandon-a-file-in-the-middle-of-a-scan-and-switch-to-a-new-file_003f">How do I abandon a file in the middle of a scan and switch to a new file?</a>
<li><a href="#How-do-I-execute-code-only-during-initialization-_0028only-before-the-first-scan_0029_003f">How do I execute code only during initialization (only before the first scan)?</a>
<li><a href="#How-do-I-execute-code-at-termination_003f">How do I execute code at termination?</a>
<li><a href="#Where-else-can-I-find-help_003f">Where else can I find help?</a>
<li><a href="#Can-I-include-comments-in-the-_0022rules_0022-section-of-the-file_003f">Can I include comments in the "rules" section of the file?</a>
<li><a href="#I-get-an-error-about-undefined-yywrap_0028_0029_002e">I get an error about undefined yywrap().</a>
<li><a href="#How-can-I-change-the-matching-pattern-at-run-time_003f">How can I change the matching pattern at run time?</a>
<li><a href="#How-can-I-expand-macros-in-the-input_003f">How can I expand macros in the input?</a>
<li><a href="#How-can-I-build-a-two_002dpass-scanner_003f">How can I build a two-pass scanner?</a>
<li><a href="#How-do-I-match-any-string-not-matched-in-the-preceding-rules_003f">How do I match any string not matched in the preceding rules?</a>
<li><a href="#I-am-trying-to-port-code-from-AT_0026T-lex-that-uses-yysptr-and-yysbuf_002e">I am trying to port code from AT&T lex that uses yysptr and yysbuf.</a>
<li><a href="#Is-there-a-way-to-make-flex-treat-NULL-like-a-regular-character_003f">Is there a way to make flex treat NULL like a regular character?</a>
<li><a href="#Whenever-flex-can-not-match-the-input-it-says-_0022flex-scanner-jammed_0022_002e">Whenever flex can not match the input it says "flex scanner jammed".</a>
<li><a href="#Why-doesnt-flex-have-non_002dgreedy-operators-like-perl-does_003f">Why doesn't flex have non-greedy operators like perl does?</a>
<li><a href="#Memory-leak-_002d-16386-bytes-allocated-by-malloc_002e">Memory leak - 16386 bytes allocated by malloc.</a>
<li><a href="#How-do-I-track-the-byte-offset-for-lseek_0028_0029_003f">How do I track the byte offset for lseek()?</a>
<li><a href="#How-do-I-use-my-own-I_002fO-classes-in-a-C_002b_002b-scanner_003f">How do I use my own I/O classes in a C++ scanner?</a>
<li><a href="#How-do-I-skip-as-many-chars-as-possible_003f">How do I skip as many chars as possible?</a>
<li><a href="#deleteme00">deleteme00</a>
<li><a href="#Are-certain-equivalent-patterns-faster-than-others_003f">Are certain equivalent patterns faster than others?</a>
<li><a href="#Is-backing-up-a-big-deal_003f">Is backing up a big deal?</a>
<li><a href="#Can-I-fake-multi_002dbyte-character-support_003f">Can I fake multi-byte character support?</a>
<li><a href="#deleteme01">deleteme01</a>
<li><a href="#Can-you-discuss-some-flex-internals_003f">Can you discuss some flex internals?</a>
<li><a href="#unput_0028_0029-messes-up-yy_005fat_005fbol">unput() messes up yy_at_bol</a>
<li><a href="#The-_007c-operator-is-not-doing-what-I-want">The | operator is not doing what I want</a>
<li><a href="#Why-can_0027t-flex-understand-this-variable-trailing-context-pattern_003f">Why can't flex understand this variable trailing context pattern?</a>
<li><a href="#Trailing-context-is-getting-confused-with-trailing-optional-patterns">Trailing context is getting confused with trailing optional patterns</a>
<li><a href="#Is-flex-GNU-or-not_003f">Is flex GNU or not?</a>
<li><a href="#ERASEME53">ERASEME53</a>
<li><a href="#I-need-to-scan-if_002dthen_002delse-blocks-and-while-loops">I need to scan if-then-else blocks and while loops</a>
<li><a href="#ERASEME55">ERASEME55</a>
<li><a href="#ERASEME56">ERASEME56</a>
<li><a href="#ERASEME57">ERASEME57</a>
<li><a href="#Is-there-a-repository-for-flex-scanners_003f">Is there a repository for flex scanners?</a>
<li><a href="#How-can-I-conditionally-compile-or-preprocess-my-flex-input-file_003f">How can I conditionally compile or preprocess my flex input file?</a>
<li><a href="#Where-can-I-find-grammars-for-lex-and-yacc_003f">Where can I find grammars for lex and yacc?</a>
<li><a href="#I-get-an-end_002dof_002dbuffer-message-for-each-character-scanned_002e">I get an end-of-buffer message for each character scanned.</a>
<li><a href="#When-was-flex-born_003f">When was flex born?</a>
<li><a href="#How-do-I-expand-_005c-escape-sequences-in-C_002dstyle-quoted-strings_003f">How do I expand \ escape sequences in C-style quoted strings?</a>
<li><a href="#Why-do-flex-scanners-call-fileno-if-it-is-not-ANSI-compatible_003f">Why do flex scanners call fileno if it is not ANSI compatible?</a>
<li><a href="#Does-flex-support-recursive-pattern-definitions_003f">Does flex support recursive pattern definitions?</a>
<li><a href="#How-do-I-skip-huge-chunks-of-input-_0028tens-of-megabytes_0029-while-using-flex_003f">How do I skip huge chunks of input (tens of megabytes) while using flex?</a>
<li><a href="#Flex-is-not-matching-my-patterns-in-the-same-order-that-I-defined-them_002e">Flex is not matching my patterns in the same order that I defined them.</a>
<li><a href="#My-actions-are-executing-out-of-order-or-sometimes-not-at-all_002e">My actions are executing out of order or sometimes not at all.</a>
<li><a href="#How-can-I-have-multiple-input-sources-feed-into-the-same-scanner-at-the-same-time_003f">How can I have multiple input sources feed into the same scanner at the same time?</a>
<li><a href="#Can-I-build-nested-parsers-that-work-with-the-same-input-file_003f">Can I build nested parsers that work with the same input file?</a>
<li><a href="#How-can-I-match-text-only-at-the-end-of-a-file_003f">How can I match text only at the end of a file?</a>
<li><a href="#How-can-I-make-REJECT-cascade-across-start-condition-boundaries_003f">How can I make REJECT cascade across start condition boundaries?</a>
<li><a href="#Why-cant-I-use-fast-or-full-tables-with-interactive-mode_003f">Why cant I use fast or full tables with interactive mode?</a>
<li><a href="#How-much-faster-is-_002dF-or-_002df-than-_002dC_003f">How much faster is -F or -f than -C?</a>
<li><a href="#If-I-have-a-simple-grammar-cant-I-just-parse-it-with-flex_003f">If I have a simple grammar cant I just parse it with flex?</a>
<li><a href="#Why-doesnt-yyrestart_0028_0029-set-the-start-state-back-to-INITIAL_003f">Why doesnt yyrestart() set the start state back to INITIAL?</a>
<li><a href="#How-can-I-match-C_002dstyle-comments_003f">How can I match C-style comments?</a>
<li><a href="#The-period-isnt-working-the-way-I-expected_002e">The period isnt working the way I expected.</a>
<li><a href="#Can-I-get-the-flex-manual-in-another-format_003f">Can I get the flex manual in another format?</a>
<li><a href="#Does-there-exist-a-_0022faster_0022-NDFA_002d_003eDFA-algorithm_003f">Does there exist a "faster" NDFA->DFA algorithm?</a>
<li><a href="#How-does-flex-compile-the-DFA-so-quickly_003f">How does flex compile the DFA so quickly?</a>
<li><a href="#How-can-I-use-more-than-8192-rules_003f">How can I use more than 8192 rules?</a>
<li><a href="#How-do-I-abandon-a-file-in-the-middle-of-a-scan-and-switch-to-a-new-file_003f">How do I abandon a file in the middle of a scan and switch to a new file?</a>
<li><a href="#How-do-I-execute-code-only-during-initialization-_0028only-before-the-first-scan_0029_003f">How do I execute code only during initialization (only before the first scan)?</a>
<li><a href="#How-do-I-execute-code-at-termination_003f">How do I execute code at termination?</a>
<li><a href="#Where-else-can-I-find-help_003f">Where else can I find help?</a>
<li><a href="#Can-I-include-comments-in-the-_0022rules_0022-section-of-the-file_003f">Can I include comments in the "rules" section of the file?</a>
<li><a href="#I-get-an-error-about-undefined-yywrap_0028_0029_002e">I get an error about undefined yywrap().</a>
<li><a href="#How-can-I-change-the-matching-pattern-at-run-time_003f">How can I change the matching pattern at run time?</a>
<li><a href="#How-can-I-expand-macros-in-the-input_003f">How can I expand macros in the input?</a>
<li><a href="#How-can-I-build-a-two_002dpass-scanner_003f">How can I build a two-pass scanner?</a>
<li><a href="#How-do-I-match-any-string-not-matched-in-the-preceding-rules_003f">How do I match any string not matched in the preceding rules?</a>
<li><a href="#I-am-trying-to-port-code-from-AT_0026T-lex-that-uses-yysptr-and-yysbuf_002e">I am trying to port code from AT&T lex that uses yysptr and yysbuf.</a>
<li><a href="#Is-there-a-way-to-make-flex-treat-NULL-like-a-regular-character_003f">Is there a way to make flex treat NULL like a regular character?</a>
<li><a href="#Whenever-flex-can-not-match-the-input-it-says-_0022flex-scanner-jammed_0022_002e">Whenever flex can not match the input it says "flex scanner jammed".</a>
<li><a href="#Why-doesnt-flex-have-non_002dgreedy-operators-like-perl-does_003f">Why doesnt flex have non-greedy operators like perl does?</a>
<li><a href="#Memory-leak-_002d-16386-bytes-allocated-by-malloc_002e">Memory leak - 16386 bytes allocated by malloc.</a>
<li><a href="#How-do-I-track-the-byte-offset-for-lseek_0028_0029_003f">How do I track the byte offset for lseek()?</a>
<li><a href="#How-do-I-use-my-own-I_002fO-classes-in-a-C_002b_002b-scanner_003f">How do I use my own I/O classes in a C++ scanner?</a>
<li><a href="#How-do-I-skip-as-many-chars-as-possible_003f">How do I skip as many chars as possible?</a>
<li><a href="#deleteme00">deleteme00</a>
<li><a href="#Are-certain-equivalent-patterns-faster-than-others_003f">Are certain equivalent patterns faster than others?</a>
<li><a href="#Is-backing-up-a-big-deal_003f">Is backing up a big deal?</a>
<li><a href="#Can-I-fake-multi_002dbyte-character-support_003f">Can I fake multi-byte character support?</a>
<li><a href="#deleteme01">deleteme01</a>
<li><a href="#Can-you-discuss-some-flex-internals_003f">Can you discuss some flex internals?</a>
<li><a href="#unput_0028_0029-messes-up-yy_005fat_005fbol">unput() messes up yy_at_bol</a>
<li><a href="#The-_007c-operator-is-not-doing-what-I-want">The | operator is not doing what I want</a>
<li><a href="#Why-can_0027t-flex-understand-this-variable-trailing-context-pattern_003f">Why can't flex understand this variable trailing context pattern?</a>
<li><a href="#Trailing-context-is-getting-confused-with-trailing-optional-patterns">Trailing context is getting confused with trailing optional patterns</a>
<li><a href="#Is-flex-GNU-or-not_003f">Is flex GNU or not?</a>
<li><a href="#ERASEME53">ERASEME53</a>
<li><a href="#I-need-to-scan-if_002dthen_002delse-blocks-and-while-loops">I need to scan if-then-else blocks and while loops</a>
<li><a href="#ERASEME55">ERASEME55</a>
<li><a href="#ERASEME56">ERASEME56</a>
<li><a href="#ERASEME57">ERASEME57</a>
<li><a href="#Is-there-a-repository-for-flex-scanners_003f">Is there a repository for flex scanners?</a>
<li><a href="#How-can-I-conditionally-compile-or-preprocess-my-flex-input-file_003f">How can I conditionally compile or preprocess my flex input file?</a>
<li><a href="#Where-can-I-find-grammars-for-lex-and-yacc_003f">Where can I find grammars for lex and yacc?</a>
<li><a href="#I-get-an-end_002dof_002dbuffer-message-for-each-character-scanned_002e">I get an end-of-buffer message for each character scanned.</a>
<p><a name="index-copyright-of-flex-1"></a><a name="index-distributing-flex-2"></a>The flex manual is placed under the same licensing conditions as the
<p>First some simple examples to get the flavor of how one uses
<code>flex</code>.
<p><a name="index-username-expansion-6"></a>The following <code>flex</code> input specifies a scanner which, when it
encounters the string `<samp><span class="samp">username</span></samp>' will replace it with the user's
login name:
<pre class="example"><pre class="verbatim">
%%
username printf( "%s", getlogin() );
</pre>
</pre>
<p><a name="index-default-rule-7"></a><a name="index-rules_002c-default-8"></a>By default, any text not matched by a <code>flex</code> scanner is copied to
the output, so the net effect of this scanner is to copy its input file
to its output with each occurrence of `<samp><span class="samp">username</span></samp>' expanded. In this
input, there is just one rule. `<samp><span class="samp">username</span></samp>' is the <dfn>pattern</dfn> and
the `<samp><span class="samp">printf</span></samp>' is the <dfn>action</dfn>. The `<samp><span class="samp">%%</span></samp>' symbol marks the
<p>and matches one-or-more digits followed by a `<samp><span class="samp">.</span></samp>' followed by
zero-or-more digits.
<p><a name="index-comments-in-flex-input-23"></a>An unindented comment (i.e., a line
beginning with `<samp><span class="samp">/*</span></samp>') is copied verbatim to the output up
to the next `<samp><span class="samp">*/</span></samp>'.
<p><a name="index-g_t_0025_0040_007b-and-_0025_0040_007d_002c-in-Definitions-Section-24"></a><a name="index-embedding-C-code-in-flex-input-25"></a><a name="index-C-code-in-flex-input-26"></a>Any <em>indented</em> text or text enclosed in `<samp><span class="samp">%{</span></samp>' and `<samp><span class="samp">%}</span></samp>'
is also copied verbatim to the output (with the %{ and %} symbols
removed). The %{ and %} symbols must appear unindented on lines by
themselves.
<p><a name="index-g_t_0025top-27"></a>
A <code>%top</code> block is similar to a `<samp><span class="samp">%{</span></samp>' ... `<samp><span class="samp">%}</span></samp>' block, except
that the code in a <code>%top</code> block is relocated to the <em>top</em> of the
generated file, before any flex definitions <a rel="footnote" href="#fn-1" name="fnd-1"><sup>1</sup></a>.
The <code>%top</code> block is useful when you want certain preprocessor macros to be
defined or certain files to be included before the generated code.
The single characters, `<samp><span class="samp">{</span></samp>' and `<samp><span class="samp">}</span></samp>' are used to delimit the
<code>%top</code> block, as show in the example below:
<pre class="example"><pre class="verbatim">
%top{
/* This code goes at the "top" of the generated file. */
#include <stdint.h>
#include <inttypes.h>
}
</pre>
</pre>
<p>Multiple <code>%top</code> blocks are allowed, and their order is preserved.
<h3 class="section">5.2 Format of the Rules Section</h3>
<p><a name="index-input-file_002c-Rules-Section-28"></a><a name="index-rules_002c-in-flex-input-29"></a>The <dfn>rules</dfn> section of the <code>flex</code> input contains a series of
rules of the form:
<pre class="example"><pre class="verbatim">
pattern action
</pre>
</pre>
<p>where the pattern must be unindented and the action must begin
on the same line.
See <a href="#Patterns">Patterns</a>, for a further description of patterns and actions.
<p>In the rules section, any indented or %{ %} enclosed text appearing
before the first rule may be used to declare variables which are local
to the scanning routine and (after the declarations) code which is to be
executed whenever the scanning routine is entered. Other indented or
%{ %} text in the rule section is still copied to the output, but its
meaning is not well-defined and it may well cause compile-time errors
(this feature is present for <acronym>POSIX</acronym> compliance. See <a href="#Lex-and-Posix">Lex and Posix</a>, for other such features).
<p>Any <em>indented</em> text or text enclosed in `<samp><span class="samp">%{</span></samp>' and `<samp><span class="samp">%}</span></samp>'
is copied verbatim to the output (with the %{ and %} symbols removed).
The %{ and %} symbols must appear unindented on lines by themselves.
<div class="node">
<p><hr>
<a name="User-Code-Section"></a>
Next: <a rel="next" accesskey="n" href="#Comments-in-the-Input">Comments in the Input</a>,
<h3 class="section">5.3 Format of the User Code Section</h3>
<p><a name="index-input-file_002c-user-code-Section-30"></a><a name="index-user-code_002c-in-flex-input-31"></a>The user code section is simply copied to <samp><span class="file">lex.yy.c</span></samp> verbatim. It
is used for companion routines which call or are called by the scanner.
The presence of this section is optional; if it is missing, the second
`<samp><span class="samp">%%</span></samp>' in the input file may be skipped, too.
<p><a name="index-patterns_002c-in-rules-section-36"></a><a name="index-regular-expressions_002c-in-patterns-37"></a>The patterns in the input (see <a href="#Rules-Section">Rules Section</a>) are written using an
<dt>`<samp><span class="samp">x</span></samp>'<dd>match the character 'x'
<br><dt>`<samp><span class="samp">.</span></samp>'<dd>any character (byte) except newline
<p><a name="index-g_t_005b_005d-in-patterns-40"></a><a name="index-character-classes-in-patterns_002c-syntax-of-41"></a><a name="index-POSIX_002c-character-classes-in-patterns_002c-syntax-of-42"></a><br><dt>`<samp><span class="samp">[xyz]</span></samp>'<dd>a <dfn>character class</dfn>; in this case, the pattern
matches either an 'x', a 'y', or a 'z'
<p><a name="index-ranges-in-patterns-43"></a><br><dt>`<samp><span class="samp">[abj-oZ]</span></samp>'<dd>a "character class" with a range in it; matches
an 'a', a 'b', any letter from 'j' through 'o',
or a 'Z'
<p><a name="index-ranges-in-patterns_002c-negating-44"></a><a name="index-negating-ranges-in-patterns-45"></a><br><dt>`<samp><span class="samp">[^A-Z]</span></samp>'<dd>a "negated character class", i.e., any character
but those in the class. In this case, any
character EXCEPT an uppercase letter.
<br><dt>`<samp><span class="samp">[^A-Z\n]</span></samp>'<dd>any character EXCEPT an uppercase letter or
a newline
<br><dt>`<samp><span class="samp">r*</span></samp>'<dd>zero or more r's, where r is any regular expression
<br><dt>`<samp><span class="samp">r+</span></samp>'<dd>one or more r's
<br><dt>`<samp><span class="samp">r?</span></samp>'<dd>zero or one r's (that is, “an optional r”)
<p><a name="index-braces-in-patterns-46"></a><br><dt>`<samp><span class="samp">r{2,5}</span></samp>'<dd>anywhere from two to five r's
<br><dt>`<samp><span class="samp">r{2,}</span></samp>'<dd>two or more r's
<p><a name="index-pattern-aliases_002c-expansion-of-47"></a><br><dt>`<samp><span class="samp">{name}</span></samp>'<dd>the expansion of the `<samp><span class="samp">name</span></samp>' definition
<p><a name="index-escape-sequences-in-patterns_002c-syntax-of-50"></a><br><dt>`<samp><span class="samp">\X</span></samp>'<dd>if X is `<samp><span class="samp">a</span></samp>', `<samp><span class="samp">b</span></samp>', `<samp><span class="samp">f</span></samp>', `<samp><span class="samp">n</span></samp>', `<samp><span class="samp">r</span></samp>', `<samp><span class="samp">t</span></samp>', or
`<samp><span class="samp">v</span></samp>', then the ANSI-C interpretation of `<samp><span class="samp">\x</span></samp>'. Otherwise, a
literal `<samp><span class="samp">X</span></samp>' (used to escape operators such as `<samp><span class="samp">*</span></samp>')
<p><a name="index-NULL-character-in-patterns_002c-syntax-of-51"></a><br><dt>`<samp><span class="samp">\0</span></samp>'<dd>a NUL character (ASCII code 0)
<p><a name="index-octal-characters-in-patterns-52"></a><br><dt>`<samp><span class="samp">\123</span></samp>'<dd>the character with octal value 123
<br><dt>`<samp><span class="samp">\x2a</span></samp>'<dd>the character with hexadecimal value 2a
<br><dt>`<samp><span class="samp">(r)</span></samp>'<dd>match an `<samp><span class="samp">r</span></samp>'; parentheses are used to override precedence (see below)
<p><a name="index-concatenation_002c-in-patterns-53"></a><br><dt>`<samp><span class="samp">rs</span></samp>'<dd>the regular expression `<samp><span class="samp">r</span></samp>' followed by the regular expression `<samp><span class="samp">s</span></samp>'; called
<dfn>concatenation</dfn>
<br><dt>`<samp><span class="samp">r|s</span></samp>'<dd>either an `<samp><span class="samp">r</span></samp>' or an `<samp><span class="samp">s</span></samp>'
<p><a name="index-trailing-context_002c-in-patterns-54"></a><br><dt>`<samp><span class="samp">r/s</span></samp>'<dd>an `<samp><span class="samp">r</span></samp>' but only if it is followed by an `<samp><span class="samp">s</span></samp>'. The text matched by `<samp><span class="samp">s</span></samp>' is
included when determining whether this rule is the longest match, but is
then returned to the input before the action is executed. So the action
only sees the text matched by `<samp><span class="samp">r</span></samp>'. This type of pattern is called
<dfn>trailing context</dfn>. (There are some combinations of `<samp><span class="samp">r/s</span></samp>' that flex
cannot match correctly. See <a href="#Limitations">Limitations</a>, regarding dangerous trailing
context.)
<p><a name="index-beginning-of-line_002c-in-patterns-55"></a><a name="index-BOL_002c-in-patterns-56"></a><br><dt>`<samp><span class="samp">^r</span></samp>'<dd>an `<samp><span class="samp">r</span></samp>', but only at the beginning of a line (i.e.,
when just starting to scan, or right after a
newline has been scanned).
<p><a name="index-end-of-line_002c-in-patterns-57"></a><a name="index-EOL_002c-in-patterns-58"></a><br><dt>`<samp><span class="samp">r$</span></samp>'<dd>an `<samp><span class="samp">r</span></samp>', but only at the end of a line (i.e., just before a
newline). Equivalent to `<samp><span class="samp">r/\n</span></samp>'.
<p><a name="index-newline_002c-matching-in-patterns-59"></a>Note that <code>flex</code>'s notion of “newline” is exactly
whatever the C compiler used to compile <code>flex</code>
interprets `<samp><span class="samp">\n</span></samp>' as; in particular, on some DOS
systems you must either filter out `<samp><span class="samp">\r</span></samp>'s in the
input yourself, or explicitly use `<samp><span class="samp">r/\r\n</span></samp>' for `<samp><span class="samp">r$</span></samp>'.
<p><a name="index-start-conditions_002c-in-patterns-60"></a><br><dt>`<samp><span class="samp"><s>r</span></samp>'<dd>an `<samp><span class="samp">r</span></samp>', but only in start condition <code>s</code> (see <a href="#Start-Conditions">Start Conditions</a> for discussion of start conditions).
<br><dt>`<samp><span class="samp"><s1,s2,s3>r</span></samp>'<dd>same, but in any of start conditions <code>s1</code>, <code>s2</code>, or <code>s3</code>.
<br><dt>`<samp><span class="samp"><*>r</span></samp>'<dd>an `<samp><span class="samp">r</span></samp>' in any start condition, even an exclusive one.
<br><dt>`<samp><span class="samp"><s1,s2><<EOF>></span></samp>'<dd>an end-of-file when in start condition <code>s1</code> or <code>s2</code>
</dl>
<p>Note that inside of a character class, all regular expression operators
lose their special meaning except escape (`<samp><span class="samp">\</span></samp>') and the character class
operators, `<samp><span class="samp">-</span></samp>', `<samp><span class="samp">]]</span></samp>', and, at the beginning of the class, `<samp><span class="samp">^</span></samp>'.
<p><a name="index-patterns_002c-precedence-of-operators-63"></a>The regular expressions listed above are grouped according to
precedence, from highest precedence at the top to lowest at the bottom.
Those grouped together have equal precedence (see special note on the
precedence of the repeat operator, `<samp><span class="samp">{}</span></samp>', under the documentation
for the `<samp><span class="samp">--posix</span></samp>' POSIX compliance option). For example,
<p><a name="index-end-of-line_002c-in-negated-character-classes-71"></a><a name="index-EOL_002c-in-negated-character-classes-72"></a><li>A negated character class such as the example `<samp><span class="samp">[^A-Z]</span></samp>' above
<em>will</em> match a newline unless `<samp><span class="samp">\n</span></samp>' (or an equivalent escape
sequence) is one of the characters explicitly present in the negated
character class (e.g., `<samp><span class="samp">[^A-Z\n]</span></samp>'). This is unlike how many other
regular expression tools treat negated character classes, but
unfortunately the inconsistency is historically entrenched. Matching
newlines means that a pattern like `<samp><span class="samp">[^"]*</span></samp>' can match the entire
input unless there's another quote in the input.
<p><a name="index-trailing-context_002c-limits-of-73"></a><a name="index-g_t_005e-as-non_002dspecial-character-in-patterns-74"></a><a name="index-g_t_0024-as-normal-character-in-patterns-75"></a><li>A rule can have at most one instance of trailing context (the `<samp><span class="samp">/</span></samp>' operator
or the `<samp><span class="samp">$</span></samp>' operator). The start condition, `<samp><span class="samp">^</span></samp>', and `<samp><span class="samp"><<EOF>></span></samp>' patterns
can only occur at the beginning of a pattern, and, as well as with `<samp><span class="samp">/</span></samp>' and `<samp><span class="samp">$</span></samp>',
cannot be grouped inside parentheses. A `<samp><span class="samp">^</span></samp>' which does not occur at
the beginning of a rule or a `<samp><span class="samp">$</span></samp>' which does not occur at the end of
a rule loses its special properties and is treated as a normal character.
<p>Note that the first of these can be written `<samp><span class="samp">foo/bar\n</span></samp>'.
<li>The following will result in `<samp><span class="samp">$</span></samp>' or `<samp><span class="samp">^</span></samp>' being treated as a normal character:
<h2 class="chapter">7 How the Input Is Matched</h2>
<p><a name="index-patterns_002c-matching-79"></a><a name="index-input_002c-matching-80"></a><a name="index-trailing-context_002c-matching-81"></a><a name="index-matching_002c-and-trailing-context-82"></a><a name="index-matching_002c-length-of-83"></a><a name="index-matching_002c-multiple-matches-84"></a>When the generated scanner is run, it analyzes its input looking for
strings which match any of its patterns. If it finds more than one
match, it takes the one matching the most text (for trailing context
rules, this includes the length of the trailing part, even though it
will then be returned to the input). If it finds two or more matches of
the same length, the rule listed first in the <code>flex</code> input file is
chosen.
<p><a name="index-token-85"></a><a name="index-yytext-86"></a><a name="index-yyleng-87"></a>Once the match is determined, the text corresponding to the match
(called the <dfn>token</dfn>) is made available in the global character
pointer <code>yytext</code>, and its length in the global integer
<code>yyleng</code>. The <dfn>action</dfn> corresponding to the matched pattern is
then executed (see <a href="#Actions">Actions</a>), and then the remaining input is scanned
for another match.
<p><a name="index-default-rule-88"></a>If no match is found, then the <dfn>default rule</dfn> is executed: the next
character in the input is considered matched and copied to the standard
output. Thus, the simplest valid <code>flex</code> input is:
<p><a name="index-minimal-scanner-89"></a>
<pre class="example"><pre class="verbatim">
%%
</pre>
</pre>
<p>which generates a scanner that simply copies its input (one character at
a time) to its output.
<p><a name="index-yytext_002c-two-types-of-90"></a><a name="index-g_t_0025array_002c-use-of-91"></a><a name="index-g_t_0025pointer_002c-use-of-92"></a><a name="index-yytext-93"></a>Note that <code>yytext</code> can be defined in two different ways: either as
a character <em>pointer</em> or as a character <em>array</em>. You can
control which definition <code>flex</code> uses by including one of the
special directives <code>%pointer</code> or <code>%array</code> in the first
(definitions) section of your flex input. The default is
<code>%pointer</code>, unless you use the `<samp><span class="samp">-l</span></samp>' lex compatibility option,
in which case <code>yytext</code> will be an array. The advantage of using
<code>%pointer</code> is substantially faster scanning and no buffer overflow
when matching very large tokens (unless you run out of dynamic memory).
The disadvantage is that you are restricted in how your actions can
modify <code>yytext</code> (see <a href="#Actions">Actions</a>), and calls to the <code>unput()</code>
function destroys the present contents of <code>yytext</code>, which can be a
considerable porting headache when moving between different <code>lex</code>
versions.
<p><a name="index-g_t_0025array_002c-advantages-of-94"></a>The advantage of <code>%array</code> is that you can then modify <code>yytext</code>
to your heart's content, and calls to <code>unput()</code> do not destroy
<code>yytext</code> (see <a href="#Actions">Actions</a>). Furthermore, existing <code>lex</code>
programs sometimes access <code>yytext</code> externally using declarations of
the form:
<pre class="example"><pre class="verbatim">
extern char yytext[];
</pre>
</pre>
<p>This definition is erroneous when used with <code>%pointer</code>, but correct
for <code>%array</code>.
<p>The <code>%array</code> declaration defines <code>yytext</code> to be an array of
<code>YYLMAX</code> characters, which defaults to a fairly large value. You
can change the size by simply #define'ing <code>YYLMAX</code> to a different
value in the first section of your <code>flex</code> input. As mentioned
above, with <code>%pointer</code> yytext grows dynamically to accommodate
large tokens. While this means your <code>%pointer</code> scanner can
accommodate very large tokens (such as matching entire blocks of
comments), bear in mind that each time the scanner must resize
<code>yytext</code> it also must rescan the entire token from the beginning,
so matching such tokens can prove slow. <code>yytext</code> presently does
<em>not</em> dynamically grow if a call to <code>unput()</code> results in too
much text being pushed back; instead, a run-time error results.
<p><a name="index-g_t_0025array_002c-with-C_002b_002b-95"></a>Also note that you cannot use <code>%array</code> with C++ scanner classes
<p><a name="index-g_t_0025_0040_007b-and-_0025_0040_007d_002c-in-Rules-Section-100"></a><a name="index-actions_002c-use-of-_0040_007b-and-_0040_007d-101"></a><a name="index-actions_002c-embedded-C-strings-102"></a><a name="index-C_002dstrings_002c-in-actions-103"></a><a name="index-comments_002c-in-actions-104"></a>If the action contains a `<samp><span class="samp">}</span></samp>', then the action spans till the
balancing `<samp><span class="samp">}</span></samp>' is found, and the action may cross multiple lines.
<code>flex</code> knows about C strings and comments and won't be fooled by
braces found within them, but also allows actions to begin with
`<samp><span class="samp">%{</span></samp>' and will consider the action to be all the text up to the
next `<samp><span class="samp">%}</span></samp>' (regardless of ordinary braces inside the action).
<p><a name="index-g_t_007c_002c-in-actions-105"></a>An action consisting solely of a vertical bar (`<samp><span class="samp">|</span></samp>') means “same as the
action for the next rule”. See below for an illustration.
<p>Actions can include arbitrary C code, including <code>return</code> statements
to return a value to whatever routine called <code>yylex()</code>. Each time
<code>yylex()</code> is called it continues processing tokens from where it
last left off until it either reaches the end of the file or executes a
return.
<p><a name="index-yytext_002c-modification-of-106"></a>Actions are free to modify <code>yytext</code> except for lengthening it
(adding characters to its end–these will overwrite later characters in
the input stream). This however does not apply when using <code>%array</code>
(see <a href="#Matching">Matching</a>). In that case, <code>yytext</code> may be freely modified
in any way.
<p><a name="index-yyleng_002c-modification-of-107"></a><a name="index-yymore_002c-and-yyleng-108"></a>Actions are free to modify <code>yyleng</code> except they should not do so if
the action also includes use of <code>yymore()</code> (see below).
<p><a name="index-preprocessor-macros_002c-for-use-in-actions-109"></a>There are a number of special directives which can be included within an
action:
<dl>
<dt><code>ECHO</code><dd><a name="index-ECHO-110"></a>copies yytext to the scanner's output.
<br><dt><code>BEGIN</code><dd><a name="index-BEGIN-111"></a>followed by the name of a start condition places the scanner in the
corresponding start condition (see below).
<br><dt><code>REJECT</code><dd><a name="index-REJECT-112"></a>directs the scanner to proceed on to the “second best” rule which
matched the input (or a prefix of the input). The rule is chosen as
described above in <a href="#Matching">Matching</a>, and <code>yytext</code> and <code>yyleng</code>
set up appropriately. It may either be one which matched as much text
as the originally chosen rule but came later in the <code>flex</code> input
file, or one which matched less text. For example, the following will
both count the words in the input and call the routine <code>special()</code>
whenever `<samp><span class="samp">frob</span></samp>' is seen:
<pre class="example"> <pre class="verbatim">
int word_count = 0;
%%
frob special(); REJECT;
[^ \t\n]+ ++word_count;
</pre>
</pre>
<p>Without the <code>REJECT</code>, any occurences of `<samp><span class="samp">frob</span></samp>' in the input
would not be counted as words, since the scanner normally executes only
one action per token. Multiple uses of <code>REJECT</code> are allowed, each
one finding the next best choice to the currently active rule. For
example, when the following scanner scans the token `<samp><span class="samp">abcd</span></samp>', it will
write `<samp><span class="samp">abcdabcaba</span></samp>' to the output:
<p>An argument of 0 to <code>yyless()</code> will cause the entire current input
string to be scanned again. Unless you've changed how the scanner will
subsequently process its input (using <code>BEGIN</code>, for example), this
will result in an endless loop.
<p>Note that <code>yyless()</code> is a macro and can only be used in the flex
input file, not from other source files.
<p><a name="index-unput_0028_0029-122"></a><a name="index-pushing-back-characters-with-unput-123"></a><code>unput(c)</code> puts the character <code>c</code> back onto the input stream.
It will be the next character scanned. The following action will take
the current token and cause it to be rescanned enclosed in parentheses.
<p>Note that since each <code>unput()</code> puts the given character back at the
<em>beginning</em> of the input stream, pushing back strings must be done
back-to-front.
<p><a name="index-g_t_0025pointer_002c-and-unput_0028_0029-126"></a><a name="index-unput_0028_0029_002c-and-_0025pointer-127"></a>An important potential problem when using <code>unput()</code> is that if you
are using <code>%pointer</code> (the default), a call to <code>unput()</code>
<em>destroys</em> the contents of <code>yytext</code>, starting with its
rightmost character and devouring one character to the left with each
call. If you need the value of <code>yytext</code> preserved after a call to
<code>unput()</code> (as in the above example), you must either first copy it
elsewhere, or build your scanner using <code>%array</code> instead
(see <a href="#Matching">Matching</a>).
<p><a name="index-pushing-back-EOF-128"></a><a name="index-EOF_002c-pushing-back-129"></a>Finally, note that you cannot put back `<samp><span class="samp">EOF</span></samp>' to attempt to mark the
input stream with an end-of-file.
<p><a name="index-input_0028_0029-130"></a><code>input()</code> reads the next character from the input stream. For
example, the following is one way to eat up C comments:
<p><a name="index-input_0028_0029_002c-and-C_002b_002b-133"></a><a name="index-yyinput_0028_0029-134"></a>(Note that if the scanner is compiled using <code>C++</code>, then
<code>input()</code> is instead referred to as <b>yyinput()</b>, in order to
avoid a name clash with the <code>C++</code> stream by the name of
<code>input</code>.)
<p><a name="index-flushing-the-internal-buffer-135"></a><a name="index-YY_005fFLUSH_005fBUFFER_0028_0029-136"></a><code>YY_FLUSH_BUFFER()</code> flushes the scanner's internal buffer so that
the next time the scanner attempts to match a token, it will first
refill the buffer using <code>YY_INPUT()</code> (see <a href="#Generated-Scanner">Generated Scanner</a>).
This action is a special case of the more general
<code>yy_flush_buffer()</code> function, described below (see <a href="#Multiple-Input-Buffers">Multiple Input Buffers</a>)
<p><a name="index-yyterminate_0028_0029-137"></a><a name="index-terminating-with-yyterminate_0028_0029-138"></a><a name="index-exiting-with-yyterminate_0028_0029-139"></a><a name="index-halting-with-yyterminate_0028_0029-140"></a><code>yyterminate()</code> can be used in lieu of a return statement in an
action. It terminates the scanner and returns a 0 to the scanner's
caller, indicating “all done”. By default, <code>yyterminate()</code> is
also called when an end-of-file is encountered. It is a macro and may
<p><a name="index-yylex_0028_0029_002c-in-generated-scanner-141"></a>The output of <code>flex</code> is the file <samp><span class="file">lex.yy.c</span></samp>, which contains
the scanning routine <code>yylex()</code>, a number of tables used by it for
matching tokens, and a number of auxiliary routines and macros. By
default, <code>yylex()</code> is declared as follows:
<pre class="example"><pre class="verbatim">
int yylex()
{
... various definitions and the actions in here ...
}
</pre>
</pre>
<p><a name="index-yylex_0028_0029_002c-overriding-142"></a>(If your environment supports function prototypes, then it will be
<code>int yylex( void )</code>.) This definition may be changed by defining
the <code>YY_DECL</code> macro. For example, you could use:
<p>to give the scanning routine the name <code>lexscan</code>, returning a float,
and taking two floats as arguments. Note that if you give arguments to
the scanning routine using a K&R-style/non-prototyped function
declaration, you must terminate the definition with a semi-colon (;).
<p><code>flex</code> generates `<samp><span class="samp">C99</span></samp>' function definitions by
default. However flex does have the ability to generate obsolete, er,
`<samp><span class="samp">traditional</span></samp>', function definitions. This is to support
bootstrapping gcc on old systems. Unfortunately, traditional
definitions prevent us from using any standard data types smaller than
int (such as short, char, or bool) as function arguments. For this
reason, future versions of <code>flex</code> may generate standard C99 code
only, leaving K&R-style functions to the historians. Currently, if you
do <strong>not</strong> want `<samp><span class="samp">C99</span></samp>' definitions, then you must use
<code>%option noansi-definitions</code>.
<p><a name="index-stdin_002c-default-for-yyin-144"></a><a name="index-yyin-145"></a>Whenever <code>yylex()</code> is called, it scans tokens from the global input
file <samp><span class="file">yyin</span></samp> (which defaults to stdin). It continues until it
either reaches an end-of-file (at which point it returns the value 0) or
one of its actions executes a <code>return</code> statement.
<p><a name="index-EOF-and-yyrestart_0028_0029-146"></a><a name="index-end_002dof_002dfile_002c-and-yyrestart_0028_0029-147"></a><a name="index-yyrestart_0028_0029-148"></a>If the scanner reaches an end-of-file, subsequent calls are undefined
unless either <samp><span class="file">yyin</span></samp> is pointed at a new input file (in which case
scanning continues from that file), or <code>yyrestart()</code> is called.
<code>yyrestart()</code> takes one argument, a <code>FILE *</code> pointer (which
can be NULL, if you've set up <code>YY_INPUT</code> to scan from a source other
than <code>yyin</code>), and initializes <samp><span class="file">yyin</span></samp> for scanning from that
file. Essentially there is no difference between just assigning
<samp><span class="file">yyin</span></samp> to a new input file or using <code>yyrestart()</code> to do so;
the latter is available for compatibility with previous versions of
<code>flex</code>, and because it can be used to switch input files in the
middle of scanning. It can also be used to throw away the current input
buffer, by calling it with an argument of <samp><span class="file">yyin</span></samp>; but it would be
better to use <code>YY_FLUSH_BUFFER</code> (see <a href="#Actions">Actions</a>). Note that
<code>yyrestart()</code> does <em>not</em> reset the start condition to
<code>INITIAL</code> (see <a href="#Start-Conditions">Start Conditions</a>).
<p><a name="index-RETURN_002c-within-actions-149"></a>If <code>yylex()</code> stops scanning due to executing a <code>return</code>
statement in one of the actions, the scanner may then be called again
and it will resume scanning where it left off.
<p><a name="index-YY_005fINPUT-150"></a>By default (and for purposes of efficiency), the scanner uses
block-reads rather than simple <code>getc()</code> calls to read characters
from <samp><span class="file">yyin</span></samp>. The nature of how it gets its input can be controlled
by defining the <code>YY_INPUT</code> macro. The calling sequence for
<code>YY_INPUT()</code> is <code>YY_INPUT(buf,result,max_size)</code>. Its action
is to place up to <code>max_size</code> characters in the character array
<code>buf</code> and return in the integer variable <code>result</code> either the
number of characters read or the constant <code>YY_NULL</code> (0 on Unix
systems) to indicate `<samp><span class="samp">EOF</span></samp>'. The default <code>YY_INPUT</code> reads from
the global file-pointer <samp><span class="file">yyin</span></samp>.
<p><a name="index-YY_005fINPUT_002c-overriding-151"></a>Here is a sample definition of <code>YY_INPUT</code> (in the definitions
<p>This definition will change the input processing to occur one character
at a time.
<p><a name="index-yywrap_0028_0029-152"></a>When the scanner receives an end-of-file indication from YY_INPUT, it
then checks the <code>yywrap()</code> function. If <code>yywrap()</code> returns
false (zero), then it is assumed that the function has gone ahead and
set up <samp><span class="file">yyin</span></samp> to point to another input file, and scanning
continues. If it returns true (non-zero), then the scanner terminates,
returning 0 to its caller. Note that in either case, the start
condition remains unchanged; it does <em>not</em> revert to
<code>INITIAL</code>.
<p><a name="index-yywrap_002c-default-for-153"></a><a name="index-nowrap_002c-_0025option-154"></a><a name="index-g_t_0025option-nowrap-155"></a>If you do not supply your own version of <code>yywrap()</code>, then you must
either use <code>%option noyywrap</code> (in which case the scanner behaves as
though <code>yywrap()</code> returned 1), or you must link with `<samp><span class="samp">-lfl</span></samp>' to
obtain the default version of the routine, which always returns 1.
<p>For scanning from in-memory buffers (e.g., scanning strings), see
<a href="#Scanning-Strings">Scanning Strings</a>. See <a href="#Multiple-Input-Buffers">Multiple Input Buffers</a>.
<p><a name="index-ECHO_002c-and-yyout-156"></a><a name="index-yyout-157"></a><a name="index-stdout_002c-as-default-for-yyout-158"></a>The scanner writes its <code>ECHO</code> output to the <samp><span class="file">yyout</span></samp> global
(default, <samp><span class="file">stdout</span></samp>), which may be redefined by the user simply by
assigning it to some other <code>FILE</code> pointer.
<INITIAL,STRING,QUOTE>\. { /* handle an escape ... */
...
}
</pre>
</pre>
<p>will be active only when the current start condition is either
<code>INITIAL</code>, <code>STRING</code>, or <code>QUOTE</code>.
<p><a name="index-start-conditions_002c-inclusive-v_002es_002e-exclusive-161"></a>Start conditions are declared in the definitions (first) section of the
input using unindented lines beginning with either `<samp><span class="samp">%s</span></samp>' or
`<samp><span class="samp">%x</span></samp>' followed by a list of names. The former declares
<dfn>inclusive</dfn> start conditions, the latter <dfn>exclusive</dfn> start
conditions. A start condition is activated using the <code>BEGIN</code>
action. Until the next <code>BEGIN</code> action is executed, rules with the
given start condition will be active and rules with other start
conditions will be inactive. If the start condition is inclusive, then
rules with no start conditions at all will also be active. If it is
exclusive, then <em>only</em> rules qualified with the start condition
will be active. A set of rules contingent on the same exclusive start
condition describe a scanner which is independent of any of the other
rules in the <code>flex</code> input. Because of this, exclusive start
conditions make it easy to specify “mini-scanners” which scan portions
of the input that are syntactically different from the rest (e.g.,
comments).
<p>If the distinction between inclusive and exclusive start conditions
is still a little vague, here's a simple example illustrating the
<p><a name="index-BEGIN_002c-explanation-167"></a><a name="index-BEGIN-168"></a><a name="index-INITIAL-169"></a><code>BEGIN(0)</code> returns to the original state where only the rules with
no start conditions are active. This state can also be referred to as
the start-condition <code>INITIAL</code>, so <code>BEGIN(INITIAL)</code> is
equivalent to <code>BEGIN(0)</code>. (The parentheses around the start
condition name are not required but are considered good style.)
<p><code>BEGIN</code> actions can also be given as indented code at the beginning
of the rules section. For example, the following will cause the scanner
to enter the <code>SPECIAL</code> start condition whenever <code>yylex()</code> is
called and the global variable <code>enter_special</code> is true:
<p>is an alias for <code>yy_create_buffer()</code>,
provided for compatibility with the C++ use of <code>new</code> and
<code>delete</code> for creating and destroying dynamic objects.
<p><a name="index-YY_005fCURRENT_005fBUFFER_002c-and-multiple-buffers-Finally_002c-the-macro-205"></a><code>YY_CURRENT_BUFFER</code> macro returns a <code>YY_BUFFER_STATE</code> handle to the
current buffer. It should not be used as an lvalue.
<p><a name="index-EOF_002c-example-using-multiple-input-buffers-206"></a>Here are two examples of using these features for writing a scanner
which expands include files (the
<code><<EOF>></code>
feature is discussed below).
<p>This first example uses yypush_buffer_state and yypop_buffer_state. Flex
<p><a name="index-EOF_002c-explanation-215"></a>The special rule <code><<EOF>></code> indicates
actions which are to be taken when an end-of-file is
encountered and <code>yywrap()</code> returns non-zero (i.e., indicates
no further files to process). The action must finish
by doing one of the following things:
<ul>
<li><a name="index-YY_005fNEW_005fFILE--_0028now-obsolete_0029-216"></a>assigning <samp><span class="file">yyin</span></samp> to a new input file (in previous versions of
<code>flex</code>, after doing the assignment you had to call the special
action <code>YY_NEW_FILE</code>. This is no longer necessary.)
<li>executing a <code>return</code> statement;
<li>executing the special <code>yyterminate()</code> action.
<li>or, switching to a new buffer using <code>yy_switch_to_buffer()</code> as
shown in the example above.
</ul>
<p><<EOF>> rules may not be used with other patterns; they may only be
qualified with a list of start conditions. If an unqualified <<EOF>>
rule is given, it applies to <em>all</em> start conditions which do not
already have <<EOF>> actions. To specify an <<EOF>> rule for only the
initial start condition, use:
<pre class="example"><pre class="verbatim">
<INITIAL><<EOF>>
</pre>
</pre>
<p>These rules are useful for catching things like unclosed comments. An
<p><a name="index-YY_005fNUM_005fRULES-220"></a>where <code>ctr</code> is an array to hold the counts for the different rules.
Note that the macro <code>YY_NUM_RULES</code> gives the total number of rules
(including the default rule), even if you use `<samp><span class="samp">-s)</span></samp>', so a correct
declaration for <code>ctr</code> is:
<pre class="example"><pre class="verbatim">
int ctr[YY_NUM_RULES];
</pre>
</pre>
<p><a name="index-YY_005fUSER_005fINIT-221"></a>The macro <code>YY_USER_INIT</code> may be defined to provide an action which
is always executed before the first scan (and before the scanner's
internal initializations are done). For example, it could be used to
call a routine to read in a data table or open a logging file.
<p><a name="index-yy_005fset_005finteractive-222"></a>The macro <code>yy_set_interactive(is_interactive)</code> can be used to
control whether the current buffer is considered <dfn>interactive</dfn>. An
interactive buffer is processed more slowly, but must be used when the
scanner's input source is indeed interactive to avoid problems due to
waiting to fill buffers (see the discussion of the `<samp><span class="samp">-I</span></samp>' flag in
<a href="#Scanner-Options">Scanner Options</a>). A non-zero value in the macro invocation marks
the buffer as interactive, a zero value as non-interactive. Note that
use of this macro overrides <code>%option always-interactive</code> or
<code>%option never-interactive</code> (see <a href="#Scanner-Options">Scanner Options</a>).
<code>yy_set_interactive()</code> must be invoked prior to beginning to scan
the buffer that is (or is not) to be considered interactive.
<p><a name="index-BOL_002c-setting-it-223"></a><a name="index-yy_005fset_005fbol-224"></a>The macro <code>yy_set_bol(at_bol)</code> can be used to control whether the
current buffer's scanning context for the next token match is done as
though at the beginning of a line. A non-zero macro argument makes
rules anchored with `<samp><span class="samp">^</span></samp>' active, while a zero argument makes
<p><a name="index-BOL_002c-checking-the-BOL-flag-225"></a><a name="index-YY_005fAT_005fBOL-226"></a>The macro <code>YY_AT_BOL()</code> returns true if the next token scanned from
the current buffer will have `<samp><span class="samp">^</span></samp>' rules active, false otherwise.
<p><a name="index-actions_002c-redefining-YY_005fBREAK-227"></a><a name="index-YY_005fBREAK-228"></a>In the generated scanner, the actions are all gathered in one large
switch statement and separated using <code>YY_BREAK</code>, which may be
redefined. By default, it is simply a <code>break</code>, to separate each
rule's action from the following rule's. Redefining <code>YY_BREAK</code>
allows, for example, C++ users to #define YY_BREAK to do nothing (while
being very careful that every rule ends with a <code>break</code>" or a
<code>return</code>!) to avoid suffering from unreachable statement warnings
where because a rule's action ends with <code>return</code>, the
<h2 class="chapter">14 Values Available To the User</h2>
<p>This chapter summarizes the various values available to the user in the
rule actions.
<a name="index-yytext-229"></a>
<dl><dt><code>char *yytext</code><dd>holds the text of the current token. It may be modified but not
lengthened (you cannot append characters to the end).
<p><a name="index-yytext_002c-default-array-size-230"></a><a name="index-array_002c-default-size-for-yytext-231"></a><a name="index-YYLMAX-232"></a>If the special directive <code>%array</code> appears in the first section of
the scanner description, then <code>yytext</code> is instead declared
<code>char yytext[YYLMAX]</code>, where <code>YYLMAX</code> is a macro definition
that you can redefine in the first section if you don't like the default
value (generally 8KB). Using <code>%array</code> results in somewhat slower
scanners, but the value of <code>yytext</code> becomes immune to calls to
<code>unput()</code>, which potentially destroy its value when <code>yytext</code> is
a character pointer. The opposite of <code>%array</code> is <code>%pointer</code>,
which is the default.
<p><a name="index-C_002b_002b-and-_0025array-233"></a>You cannot use <code>%array</code> when generating C++ scanner classes (the
<p><a name="index-yyleng-234"></a><br><dt><code>int yyleng</code><dd>holds the length of the current token.
<p><a name="index-yyin-235"></a><br><dt><code>FILE *yyin</code><dd>is the file which by default <code>flex</code> reads from. It may be
redefined but doing so only makes sense before scanning begins or after
an EOF has been encountered. Changing it in the midst of scanning will
have unexpected results since <code>flex</code> buffers its input; use
<code>yyrestart()</code> instead. Once scanning terminates because an
end-of-file has been seen, you can assign <samp><span class="file">yyin</span></samp> at the new input
file and then call the scanner again to continue scanning.
<p><a name="index-yyrestart-236"></a><br><dt><code>void yyrestart( FILE *new_file )</code><dd>may be called to point <samp><span class="file">yyin</span></samp> at the new input file. The
switch-over to the new file is immediate (any previously buffered-up
input is lost). Note that calling <code>yyrestart()</code> with <samp><span class="file">yyin</span></samp>
as an argument thus throws away the current input buffer and continues
scanning the same input file.
<p><a name="index-yyout-237"></a><br><dt><code>FILE *yyout</code><dd>is the file to which <code>ECHO</code> actions are done. It can be reassigned
by the user.
<p><a name="index-YY_005fCURRENT_005fBUFFER-238"></a><br><dt><code>YY_CURRENT_BUFFER</code><dd>returns a <code>YY_BUFFER_STATE</code> handle to the current buffer.
<p><a name="index-YY_005fSTART-239"></a><br><dt><code>YY_START</code><dd>returns an integer value corresponding to the current start condition.
You can subsequently use this value with <code>BEGIN</code> to return to that
<h3 class="section">16.1 Options for Specifing Filenames</h3>
<dl>
<a name="option_002dheader"></a>
<a name="index-g_t_002d_002d_002dheader_002dfile-246"></a><a name="index-header_002dfile-247"></a><dt>`<samp><span class="samp">--header-file=FILE, </span><code>%option header-file="FILE"</code></samp>'<dd>instructs flex to write a C header to <samp><span class="file">FILE</span></samp>. This file contains
function prototypes, extern variables, and types used by the scanner.
Only the external API is exported by the header file. Many macros that
are usable from within scanner actions are not exported to the header
file. This is due to namespace problems and the goal of a clean
external API.
<p>While in the header, the macro <code>yyIN_HEADER</code> is defined, where `<samp><span class="samp">yy</span></samp>'
is substituted with the appropriate prefix.
<p>The `<samp><span class="samp">--header-file</span></samp>' option is not compatible with the `<samp><span class="samp">--c++</span></samp>' option,
since the C++ scanner provides its own header in <samp><span class="file">yyFlexLexer.h</span></samp>.
<p><a name="option_002doutfile"></a>
<a name="index-g_t_002do-248"></a><a name="index-g_t_002d_002d_002doutfile-249"></a><a name="index-outfile-250"></a><br><dt>`<samp><span class="samp">-oFILE, --outfile=FILE, </span><code>%option outfile="FILE"</code></samp>'<dd>directs flex to write the scanner to the file <samp><span class="file">FILE</span></samp> instead of
<samp><span class="file">lex.yy.c</span></samp>. If you combine `<samp><span class="samp">--outfile</span></samp>' with the `<samp><span class="samp">--stdout</span></samp>' option,
then the scanner is written to <samp><span class="file">stdout</span></samp> but its <code>#line</code>
directives (see the `<samp><span class="samp">-l</span></samp>' option above) refer to the file
<samp><span class="file">FILE</span></samp>.
<p><a name="option_002dstdout"></a>
<a name="index-g_t_002dt-251"></a><a name="index-g_t_002d_002d_002dstdout-252"></a><a name="index-stdout-253"></a><br><dt>`<samp><span class="samp">-t, --stdout, </span><code>%option stdout</code></samp>'<dd>instructs <code>flex</code> to write the scanner it generates to standard
output instead of <samp><span class="file">lex.yy.c</span></samp>.
<p><a name="index-g_t_002d_002d_002dskel-254"></a><br><dt>`<samp><span class="samp">-SFILE, --skel=FILE</span></samp>'<dd>overrides the default skeleton file from which
<code>flex</code>
constructs its scanners. You'll never need this option unless you are doing
<code>flex</code>
maintenance or development.
<p><a name="index-g_t_002d_002d_002dtables_002dfile-255"></a><a name="index-tables_002dfile-256"></a><br><dt>`<samp><span class="samp">--tables-file=FILE</span></samp>'<dd>Write serialized scanner dfa tables to FILE. The generated scanner will not
contain the tables, and requires them to be loaded at runtime.
See <a href="#serialization">serialization</a>.
<p><a name="index-g_t_002d_002d_002dtables_002dverify-257"></a><a name="index-tables_002dverify-258"></a><br><dt>`<samp><span class="samp">--tables-verify</span></samp>'<dd>This option is for flex development. We document it here in case you stumble
upon it by accident or in case you suspect some inconsistency in the serialized
tables. Flex will serialize the scanner dfa tables but will also generate the
in-code tables as it normally does. At runtime, the scanner will verify that
the serialized tables match the in-code tables, instead of loading them.
</dl>
<div class="node">
<p><hr>
<a name="Options-Affecting-Scanner-Behavior"></a>
Next: <a rel="next" accesskey="n" href="#Code_002dLevel-And-API-Options">Code-Level And API Options</a>,
Previous: <a rel="previous" accesskey="p" href="#Options-for-Specifing-Filenames">Options for Specifing Filenames</a>,
<a name="index-g_t_002di-259"></a><a name="index-g_t_002d_002d_002dcase_002dinsensitive-260"></a><a name="index-case_002dinsensitive-261"></a><dt>`<samp><span class="samp">-i, --case-insensitive, </span><code>%option case-insensitive</code></samp>'<dd>instructs <code>flex</code> to generate a <dfn>case-insensitive</dfn> scanner. The
case of letters given in the <code>flex</code> input patterns will be ignored,
and tokens in the input will be matched regardless of case. The matched
text given in <code>yytext</code> will have the preserved case (i.e., it will
not be folded). For tricky behavior, see <a href="#case-and-character-ranges">case and character ranges</a>.
<p><a name="option_002dlex_002dcompat"></a>
<a name="index-g_t_002dl-262"></a><a name="index-g_t_002d_002d_002dlex_002dcompat-263"></a><a name="index-lex_002dcompat-264"></a><br><dt>`<samp><span class="samp">-l, --lex-compat, </span><code>%option lex-compat</code></samp>'<dd>turns on maximum compatibility with the original AT&T <code>lex</code>
implementation. Note that this does not mean <em>full</em> compatibility.
Use of this option costs a considerable amount of performance, and it
cannot be used with the `<samp><span class="samp">--c++</span></samp>', `<samp><span class="samp">--full</span></samp>', `<samp><span class="samp">--fast</span></samp>', `<samp><span class="samp">-Cf</span></samp>', or
`<samp><span class="samp">-CF</span></samp>' options. For details on the compatibilities it provides, see
<a href="#Lex-and-Posix">Lex and Posix</a>. This option also results in the name
<code>YY_FLEX_LEX_COMPAT</code> being <code>#define</code>'d in the generated scanner.
<p><a name="option_002dbatch"></a>
<a name="index-g_t_002dB-265"></a><a name="index-g_t_002d_002d_002dbatch-266"></a><a name="index-batch-267"></a><br><dt>`<samp><span class="samp">-B, --batch, </span><code>%option batch</code></samp>'<dd>instructs <code>flex</code> to generate a <dfn>batch</dfn> scanner, the opposite of
<em>interactive</em> scanners generated by `<samp><span class="samp">--interactive</span></samp>' (see below). In
general, you use `<samp><span class="samp">-B</span></samp>' when you are <em>certain</em> that your scanner
will never be used interactively, and you want to squeeze a
<em>little</em> more performance out of it. If your goal is instead to
squeeze out a <em>lot</em> more performance, you should be using the
`<samp><span class="samp">-Cf</span></samp>' or `<samp><span class="samp">-CF</span></samp>' options, which turn on `<samp><span class="samp">--batch</span></samp>' automatically
anyway.
<p><a name="option_002dinteractive"></a>
<a name="index-g_t_002dI-268"></a><a name="index-g_t_002d_002d_002dinteractive-269"></a><a name="index-interactive-270"></a><br><dt>`<samp><span class="samp">-I, --interactive, </span><code>%option interactive</code></samp>'<dd>instructs <code>flex</code> to generate an <i>interactive</i> scanner. An
interactive scanner is one that only looks ahead to decide what token
has been matched if it absolutely must. It turns out that always
looking one extra character ahead, even if the scanner has already seen
enough text to disambiguate the current token, is a bit faster than only
looking ahead when necessary. But scanners that always look ahead give
dreadful interactive performance; for example, when a user types a
newline, it is not recognized as a newline token until they enter
<em>another</em> token, which often means typing in another whole line.
<p><code>flex</code> scanners default to <code>interactive</code> unless you use the
`<samp><span class="samp">-Cf</span></samp>' or `<samp><span class="samp">-CF</span></samp>' table-compression options
(see <a href="#Performance">Performance</a>). That's because if you're looking for
high-performance you should be using one of these options, so if you
didn't, <code>flex</code> assumes you'd rather trade off a bit of run-time
performance for intuitive interactive behavior. Note also that you
<em>cannot</em> use `<samp><span class="samp">--interactive</span></samp>' in conjunction with `<samp><span class="samp">-Cf</span></samp>' or
`<samp><span class="samp">-CF</span></samp>'. Thus, this option is not really needed; it is on by default
for all those cases in which it is allowed.
<p>You can force a scanner to
<em>not</em>
be interactive by using
`<samp><span class="samp">--batch</span></samp>'
<p><a name="option_002d7bit"></a>
<a name="index-g_t_002d7-271"></a><a name="index-g_t_002d_002d_002d7bit-272"></a><a name="index-g_t7bit-273"></a><br><dt>`<samp><span class="samp">-7, --7bit, </span><code>%option 7bit</code></samp>'<dd>instructs <code>flex</code> to generate a 7-bit scanner, i.e., one which can
only recognize 7-bit characters in its input. The advantage of using
`<samp><span class="samp">--7bit</span></samp>' is that the scanner's tables can be up to half the size of
those generated using the `<samp><span class="samp">--8bit</span></samp>'. The disadvantage is that such
scanners often hang or crash if their input contains an 8-bit character.
<p>Note, however, that unless you generate your scanner using the
`<samp><span class="samp">-Cf</span></samp>' or `<samp><span class="samp">-CF</span></samp>' table compression options, use of `<samp><span class="samp">--7bit</span></samp>'
will save only a small amount of table space, and make your scanner
considerably less portable. <code>Flex</code>'s default behavior is to
generate an 8-bit scanner unless you use the `<samp><span class="samp">-Cf</span></samp>' or `<samp><span class="samp">-CF</span></samp>',
in which case <code>flex</code> defaults to generating 7-bit scanners unless
your site was always configured to generate 8-bit scanners (as will
often be the case with non-USA sites). You can tell whether flex
generated a 7-bit or an 8-bit scanner by inspecting the flag summary in
the `<samp><span class="samp">--verbose</span></samp>' output as described above.
<p>Note that if you use `<samp><span class="samp">-Cfe</span></samp>' or `<samp><span class="samp">-CFe</span></samp>' <code>flex</code> still
defaults to generating an 8-bit scanner, since usually with these
compression options full 8-bit tables are not much more expensive than
7-bit tables.
<p><a name="option_002d8bit"></a>
<a name="index-g_t_002d8-274"></a><a name="index-g_t_002d_002d_002d8bit-275"></a><a name="index-g_t8bit-276"></a><br><dt>`<samp><span class="samp">-8, --8bit, </span><code>%option 8bit</code></samp>'<dd>instructs <code>flex</code> to generate an 8-bit scanner, i.e., one which can
recognize 8-bit characters. This flag is only needed for scanners
generated using `<samp><span class="samp">-Cf</span></samp>' or `<samp><span class="samp">-CF</span></samp>', as otherwise flex defaults to
generating an 8-bit scanner anyway.
<p>See the discussion of
`<samp><span class="samp">--7bit</span></samp>'
above for <code>flex</code>'s default behavior and the tradeoffs between 7-bit
and 8-bit scanners.
<p><a name="option_002ddefault"></a>
<a name="index-g_t_002d_002d_002ddefault-277"></a><a name="index-default-278"></a><br><dt>`<samp><span class="samp">--default, </span><code>%option default</code></samp>'<dd>generate the default rule.
<a name="index-g_t_002d_002d_002dalways_002dinteractive-279"></a><a name="index-always_002dinteractive-280"></a><br><dt>`<samp><span class="samp">--always-interactive, </span><code>%option always-interactive</code></samp>'<dd>instructs flex to generate a scanner which always considers its input
<em>interactive</em>. Normally, on each new input file the scanner calls
<code>isatty()</code> in an attempt to determine whether the scanner's input
source is interactive and thus should be read a character at a time.
When this option is used, however, then no such call is made.
<p><a name="index-g_t_002d_002d_002dnever_002dinteractive-281"></a><br><dt>`<samp><span class="samp">--never-interactive, </span><code>--never-interactive</code></samp>'<dd>instructs flex to generate a scanner which never considers its input
interactive. This is the opposite of <code>always-interactive</code>.
<p><a name="option_002dposix"></a>
<a name="index-g_t_002dX-282"></a><a name="index-g_t_002d_002d_002dposix-283"></a><a name="index-posix-284"></a><br><dt>`<samp><span class="samp">-X, --posix, </span><code>%option posix</code></samp>'<dd>turns on maximum compatibility with the POSIX 1003.2-1992 definition of
<code>lex</code>. Since <code>flex</code> was originally designed to implement the
POSIX definition of <code>lex</code> this generally involves very few changes
in behavior. At the current writing the known differences between
<code>flex</code> and the POSIX standard are:
<ul>
<li>In POSIX and AT&T <code>lex</code>, the repeat operator, `<samp><span class="samp">{}</span></samp>', has lower
precedence than concatenation (thus `<samp><span class="samp">ab{3}</span></samp>' yields `<samp><span class="samp">ababab</span></samp>').
Most POSIX utilities use an Extended Regular Expression (ERE) precedence
that has the precedence of the repeat operator higher than concatenation
(which causes `<samp><span class="samp">ab{3}</span></samp>' to yield `<samp><span class="samp">abbb</span></samp>'). By default, <code>flex</code>
places the precedence of the repeat operator higher than concatenation
which matches the ERE processing of other POSIX utilities. When either
`<samp><span class="samp">--posix</span></samp>' or `<samp><span class="samp">-l</span></samp>' are specified, <code>flex</code> will use the
traditional AT&T and POSIX-compliant precedence for the repeat operator
where concatenation has higher precedence than the repeat operator.
</ul>
<p><a name="option_002dstack"></a>
<a name="index-g_t_002d_002d_002dstack-285"></a><a name="index-stack-286"></a><br><dt>`<samp><span class="samp">--stack, </span><code>%option stack</code></samp>'<dd>enables the use of
start condition stacks (see <a href="#Start-Conditions">Start Conditions</a>).
<p><a name="option_002dstdinit"></a>
<a name="index-g_t_002d_002d_002dstdinit-287"></a><a name="index-stdinit-288"></a><br><dt>`<samp><span class="samp">--stdinit, </span><code>%option stdinit</code></samp>'<dd>if set (i.e., <b>%option stdinit)</b> initializes <code>yyin</code> and
<code>yyout</code> to <samp><span class="file">stdin</span></samp> and <samp><span class="file">stdout</span></samp>, instead of the default of
<samp><span class="file">NULL</span></samp>. Some existing <code>lex</code> programs depend on this behavior,
even though it is not compliant with ANSI C, which does not require
<samp><span class="file">stdin</span></samp> and <samp><span class="file">stdout</span></samp> to be compile-time constant. In a
reentrant scanner, however, this is not a problem since initialization
is performed in <code>yylex_init</code> at runtime.
<p><a name="option_002dyylineno"></a>
<a name="index-g_t_002d_002d_002dyylineno-289"></a><a name="index-yylineno-290"></a><br><dt>`<samp><span class="samp">--yylineno, </span><code>%option yylineno</code></samp>'<dd>directs <code>flex</code> to generate a scanner
that maintains the number of the current line read from its input in the
global variable <code>yylineno</code>. This option is implied by <code>%option
lex-compat</code>. In a reentrant C scanner, the macro <code>yylineno</code> is
accessible regardless of the value of <code>%option yylineno</code>, however, its
value is not modified by <code>flex</code> unless <code>%option yylineno</code> is enabled.
<p><a name="option_002dyywrap"></a>
<a name="index-g_t_002d_002d_002dyywrap-291"></a><a name="index-yywrap-292"></a><br><dt>`<samp><span class="samp">--yywrap, </span><code>%option yywrap</code></samp>'<dd>if unset (i.e., <code>--noyywrap)</code>, makes the scanner not call
<code>yywrap()</code> upon an end-of-file, but simply assume that there are no
more files to scan (until the user points <samp><span class="file">yyin</span></samp> at a new file and
calls <code>yylex()</code> again).
</dl>
<div class="node">
<p><hr>
<a name="Code-Level-And-API-Options"></a>
<a name="Code_002dLevel-And-API-Options"></a>
Next: <a rel="next" accesskey="n" href="#Options-for-Scanner-Speed-and-Size">Options for Scanner Speed and Size</a>,
<h3 class="section">16.3 Code-Level And API Options</h3>
<dl>
<a name="option_002dansi_002ddefinitions"></a>
<a name="index-g_t_002d_002d_002doption_002dansi_002ddefinitions-293"></a><a name="index-ansi_002ddefinitions-294"></a><dt>`<samp><span class="samp">--ansi-definitions, </span><code>%option ansi-definitions</code></samp>'<dd>instruct flex to generate ANSI C99 definitions for functions.
This option is enabled by default.
If <code>%option noansi-definitions</code> is specified, then the obsolete style
is generated.
<p><a name="option_002dansi_002dprototypes"></a>
<a name="index-g_t_002d_002d_002doption_002dansi_002dprototypes-295"></a><a name="index-ansi_002dprototypes-296"></a><br><dt>`<samp><span class="samp">--ansi-prototypes, </span><code>%option ansi-prototypes</code></samp>'<dd>instructs flex to generate ANSI C99 prototypes for functions.
This option is enabled by default.
If <code>noansi-prototypes</code> is specified, then
prototypes will have empty parameter lists.
<p><a name="option_002dbison_002dbridge"></a>
<a name="index-g_t_002d_002d_002dbison_002dbridge-297"></a><a name="index-bison_002dbridge-298"></a><br><dt>`<samp><span class="samp">--bison-bridge, </span><code>%option bison-bridge</code></samp>'<dd>instructs flex to generate a C scanner that is
meant to be called by a
<code>GNU bison</code>
parser. The scanner has minor API changes for
<code>bison</code>
compatibility. In particular, the declaration of
<code>yylex</code>
is modified to take an additional parameter,
<code>yylval</code>.
See <a href="#Bison-Bridge">Bison Bridge</a>.
<p><a name="option_002dbison_002dlocations"></a>
<a name="index-g_t_002d_002d_002dbison_002dlocations-299"></a><a name="index-bison_002dlocations-300"></a><br><dt>`<samp><span class="samp">--bison-locations, </span><code>%option bison-locations</code></samp>'<dd>instruct flex that
<code>GNU bison</code> <code>%locations</code> are being used.
This means <code>yylex</code> will be passed
an additional parameter, <code>yylloc</code>. This option
with <code>#line</code> directives so error messages in the actions will be correctly
located with respect to either the original
<code>flex</code>
input file (if the errors are due to code in the input file), or
<samp><span class="file">lex.yy.c</span></samp>
(if the errors are
<code>flex</code>'s
fault – you should report these sorts of errors to the email address
given in <a href="#Reporting-Bugs">Reporting Bugs</a>).
<p><a name="option_002dreentrant"></a>
<a name="index-g_t_002dR-304"></a><a name="index-g_t_002d_002d_002dreentrant-305"></a><a name="index-reentrant-306"></a><br><dt>`<samp><span class="samp">-R, --reentrant, </span><code>%option reentrant</code></samp>'<dd>instructs flex to generate a reentrant C scanner. The generated scanner
may safely be used in a multi-threaded environment. The API for a
reentrant scanner is different than for a non-reentrant scanner
see <a href="#Reentrant">Reentrant</a>). Because of the API difference between
reentrant and non-reentrant <code>flex</code> scanners, non-reentrant flex
code must be modified before it is suitable for use with this option.
This option is not compatible with the `<samp><span class="samp">--c++</span></samp>' option.
<p>The option `<samp><span class="samp">--reentrant</span></samp>' does not affect the performance of
the scanner.
<p><a name="option_002dc_002b_002b"></a>
<a name="index-g_t_002d_002b-307"></a><a name="index-g_t_002d_002d_002dc_002b_002b-308"></a><a name="index-c_002b_002b-309"></a><br><dt>`<samp><span class="samp">-+, --c++, </span><code>%option c++</code></samp>'<dd>specifies that you want flex to generate a C++
scanner class. See <a href="#Cxx">Cxx</a>, for
details.
<p><a name="option_002darray"></a>
<a name="index-g_t_002d_002d_002darray-310"></a><a name="index-array-311"></a><br><dt>`<samp><span class="samp">--array, </span><code>%option array</code></samp>'<dd>specifies that you want yytext to be an array instead of a char*
<p><a name="option_002dpointer"></a>
<a name="index-g_t_002d_002d_002dpointer-312"></a><a name="index-pointer-313"></a><br><dt>`<samp><span class="samp">--pointer, </span><code>%option pointer</code></samp>'<dd>specify that <code>yytext</code> should be a <code>char *</code>, not an array.
This default is <code>char *</code>.
<p><a name="option_002dprefix"></a>
<a name="index-g_t_002dP-314"></a><a name="index-g_t_002d_002d_002dprefix-315"></a><a name="index-prefix-316"></a><br><dt>`<samp><span class="samp">-PPREFIX, --prefix=PREFIX, </span><code>%option prefix="PREFIX"</code></samp>'<dd>changes the default `<samp><span class="samp">yy</span></samp>' prefix used by <code>flex</code> for all
globally-visible variable and function names to instead be
`<samp><span class="samp">PREFIX</span></samp>'. For example, `<samp><span class="samp">--prefix=foo</span></samp>' changes the name of
<code>yytext</code> to <code>footext</code>. It also changes the name of the default
output file from <samp><span class="file">lex.yy.c</span></samp> to <samp><span class="file">lex.foo.c</span></samp>. Here is a partial
list of the names affected:
<pre class="example"> <pre class="verbatim">
yy_create_buffer
yy_delete_buffer
yy_flex_debug
yy_init_buffer
yy_flush_buffer
yy_load_buffer_state
yy_switch_to_buffer
yyin
yyleng
yylex
yylineno
yyout
yyrestart
yytext
yywrap
yyalloc
yyrealloc
yyfree
</pre>
</pre>
<p>(If you are using a C++ scanner, then only <code>yywrap</code> and
<code>yyFlexLexer</code> are affected.) Within your scanner itself, you can
still refer to the global variables and functions using either version
of their name; but externally, they have the modified name.
<p>This option lets you easily link together multiple
<code>flex</code>
programs into the same executable. Note, though, that using this
option also renames
<code>yywrap()</code>,
so you now
<em>must</em>
either
provide your own (appropriately-named) version of the routine for your
scanner, or use
<code>%option noyywrap</code>,
as linking with
`<samp><span class="samp">-lfl</span></samp>'
no longer provides one for you by default.
<p><a name="option_002dmain"></a>
<a name="index-g_t_002d_002d_002dmain-317"></a><a name="index-main-318"></a><br><dt>`<samp><span class="samp">--main, </span><code>%option main</code></samp>'<dd> directs flex to provide a default <code>main()</code> program for the
scanner, which simply calls <code>yylex()</code>. This option implies
<code>noyywrap</code> (see below).
<p><a name="option_002dnounistd"></a>
<a name="index-g_t_002d_002d_002dnounistd-319"></a><a name="index-nounistd-320"></a><br><dt>`<samp><span class="samp">--nounistd, </span><code>%option nounistd</code></samp>'<dd>suppresses inclusion of the non-ANSI header file <samp><span class="file">unistd.h</span></samp>. This option
is meant to target environments in which <samp><span class="file">unistd.h</span></samp> does not exist. Be aware
that certain options may cause flex to generate code that relies on functions
normally found in <samp><span class="file">unistd.h</span></samp>, (e.g. <code>isatty()</code>, <code>read()</code>.)
If you wish to use these functions, you will have to inform your compiler where
to find them.
See <a href="#option_002dalways_002dinteractive">option-always-interactive</a>. See <a href="#option_002dread">option-read</a>.
<p><a name="option_002dyyclass"></a>
<a name="index-g_t_002d_002d_002dyyclass-321"></a><a name="index-yyclass-322"></a><br><dt>`<samp><span class="samp">--yyclass, </span><code>%option yyclass="NAME"</code></samp>'<dd>only applies when generating a C++ scanner (the `<samp><span class="samp">--c++</span></samp>' option). It
informs <code>flex</code> that you have derived <code>foo</code> as a subclass of
<code>yyFlexLexer</code>, so <code>flex</code> will place your actions in the member
function <code>foo::yylex()</code> instead of <code>yyFlexLexer::yylex()</code>. It
also generates a <code>yyFlexLexer::yylex()</code> member function that emits
a run-time error (by invoking <code>yyFlexLexer::LexerError())</code> if
<h3 class="section">16.4 Options for Scanner Speed and Size</h3>
<dl>
<dt>`<samp><span class="samp">-C[aefFmr]</span></samp>'<dd>controls the degree of table compression and, more generally, trade-offs
between small scanners and fast scanners.
<a name="index-g_t_002dC-323"></a>
<dl><dt>`<samp><span class="samp">-C</span></samp>'<dd>A lone `<samp><span class="samp">-C</span></samp>' specifies that the scanner tables should be compressed
but neither equivalence classes nor meta-equivalence classes should be
used.
<p><a name="option_002dalign"></a>
<a name="index-g_t_002dCa-324"></a><a name="index-g_t_002d_002d_002dalign-325"></a><a name="index-align-326"></a><br><dt>`<samp><span class="samp">-Ca, --align, </span><code>%option align</code></samp>'<dd>(“align”) instructs flex to trade off larger tables in the
generated scanner for faster performance because the elements of
the tables are better aligned for memory access and computation. On some
RISC architectures, fetching and manipulating longwords is more efficient
than with smaller-sized units such as shortwords. This option can
quadruple the size of the tables used by your scanner.
of characters which have identical lexical properties (for example, if
the only appearance of digits in the <code>flex</code> input is in the
character class “[0-9]” then the digits '0', '1', ..., '9' will all be
put in the same equivalence class). Equivalence classes usually give
dramatic reductions in the final table/object file sizes (typically a
factor of 2-5) and are pretty cheap performance-wise (one array look-up
per character scanned).
<p><a name="index-g_t_002dCf-330"></a><br><dt>`<samp><span class="samp">-Cf</span></samp>'<dd>specifies that the <dfn>full</dfn> scanner tables should be generated -
<code>flex</code> should not compress the tables by taking advantages of
similar transition functions for different states.
<p><a name="index-g_t_002dCF-331"></a><br><dt>`<samp><span class="samp">-CF</span></samp>'<dd>specifies that the alternate fast scanner representation (described
above under the `<samp><span class="samp">--fast</span></samp>' flag) should be used. This option cannot be
used with `<samp><span class="samp">--c++</span></samp>'.
which are sets of equivalence classes (or characters, if equivalence
classes are not being used) that are commonly used together. Meta-equivalence
classes are often a big win when using compressed tables, but they
have a moderate performance impact (one or two <code>if</code> tests and one
array look-up per character scanned).
<p><a name="option_002dread"></a>
<a name="index-g_t_002dCr-335"></a><a name="index-g_t_002d_002d_002dread-336"></a><a name="index-read-337"></a><br><dt>`<samp><span class="samp">-Cr, --read, </span><code>%option read</code></samp>'<dd>causes the generated scanner to <em>bypass</em> use of the standard I/O
library (<code>stdio</code>) for input. Instead of calling <code>fread()</code> or
<code>getc()</code>, the scanner will use the <code>read()</code> system call,
resulting in a performance gain which varies from system to system, but
in general is probably negligible unless you are also using `<samp><span class="samp">-Cf</span></samp>'
or `<samp><span class="samp">-CF</span></samp>'. Using `<samp><span class="samp">-Cr</span></samp>' can cause strange behavior if, for
example, you read from <samp><span class="file">yyin</span></samp> using <code>stdio</code> prior to calling
the scanner (because the scanner will miss whatever text your previous
reads left in the <code>stdio</code> input buffer). `<samp><span class="samp">-Cr</span></samp>' has no effect
if you define <code>YY_INPUT()</code> (see <a href="#Generated-Scanner">Generated Scanner</a>).
</dl>
<p>The options `<samp><span class="samp">-Cf</span></samp>' or `<samp><span class="samp">-CF</span></samp>' and `<samp><span class="samp">-Cm</span></samp>' do not make sense
together - there is no opportunity for meta-equivalence classes if the
table is not being compressed. Otherwise the options may be freely
mixed, and are cumulative.
<p>The default setting is `<samp><span class="samp">-Cem</span></samp>', which specifies that <code>flex</code>
should generate equivalence classes and meta-equivalence classes. This
setting provides the highest degree of table compression. You can trade
off faster-executing scanners at the cost of larger tables with the
following generally being true:
<pre class="example"> <pre class="verbatim">
slowest & smallest
-Cem
-Cm
-Ce
-C
-C{f,F}e
-C{f,F}
-C{f,F}a
fastest & largest
</pre>
</pre>
<p>Note that scanners with the smallest tables are usually generated and
compiled the quickest, so during development you will usually want to
use the default, maximal compression.
<p>`<samp><span class="samp">-Cfe</span></samp>' is often a good compromise between speed and size for
No table compression is done and <code>stdio</code> is bypassed.
The result is large but fast. This option is equivalent to
`<samp><span class="samp">--Cfr</span></samp>'
<p><a name="option_002dfast"></a>
<a name="index-g_t_002dF-341"></a><a name="index-g_t_002d_002d_002dfast-342"></a><a name="index-fast-343"></a><br><dt>`<samp><span class="samp">-F, --fast, </span><code>%option fast</code></samp>'<dd>specifies that the <em>fast</em> scanner table representation should be
used (and <code>stdio</code> bypassed). This representation is about as fast
as the full table representation `<samp><span class="samp">--full</span></samp>', and for some sets of
patterns will be considerably smaller (and for others, larger). In
general, if the pattern set contains both <em>keywords</em> and a
catch-all, <em>identifier</em> rule, such as in the set:
<pre class="example"> <pre class="verbatim">
"case" return TOK_CASE;
"switch" return TOK_SWITCH;
...
"default" return TOK_DEFAULT;
[a-z]+ return TOK_ID;
</pre>
</pre>
<p>then you're better off using the full table representation. If only
the <em>identifier</em> rule is present and you then use a hash table or some such
to detect the keywords, you're better off using
`<samp><span class="samp">--fast</span></samp>'.
<p>This option is equivalent to `<samp><span class="samp">-CFr</span></samp>' (see below). It cannot be used
with `<samp><span class="samp">--c++</span></samp>'.
<a name="index-g_t_002db-344"></a><a name="index-g_t_002d_002d_002dbackup-345"></a><a name="index-backup-346"></a><dt>`<samp><span class="samp">-b, --backup, </span><code>%option backup</code></samp>'<dd>Generate backing-up information to <samp><span class="file">lex.backup</span></samp>. This is a list of
scanner states which require backing up and the input characters on
which they do so. By adding rules one can remove backing-up states. If
<em>all</em> backing-up states are eliminated and `<samp><span class="samp">-Cf</span></samp>' or <code>-CF</code>
is used, the generated scanner will run faster (see the `<samp><span class="samp">--perf-report</span></samp>' flag).
Only users who wish to squeeze every last cycle out of their scanners
need worry about this option. (see <a href="#Performance">Performance</a>).
<p><a name="option_002ddebug"></a>
<a name="index-g_t_002dd-347"></a><a name="index-g_t_002d_002d_002ddebug-348"></a><a name="index-debug-349"></a><br><dt>`<samp><span class="samp">-d, --debug, </span><code>%option debug</code></samp>'<dd>makes the generated scanner run in <dfn>debug</dfn> mode. Whenever a pattern
is recognized and the global variable <code>yy_flex_debug</code> is non-zero
(which is the default), the scanner will write to <samp><span class="file">stderr</span></samp> a line
of the form:
<pre class="example"> <pre class="verbatim">
-accepting rule at line 53 ("the matched text")
</pre>
</pre>
<p>The line number refers to the location of the rule in the file defining
the scanner (i.e., the file that was fed to flex). Messages are also
generated when the scanner backs up, accepts the default rule, reaches
the end of its input buffer (or encounters a NUL; at this point, the two
look the same as far as the scanner's concerned), or reaches an
end-of-file.
<p><a name="option_002dperf_002dreport"></a>
<a name="index-g_t_002dp-350"></a><a name="index-g_t_002d_002d_002dperf_002dreport-351"></a><a name="index-perf_002dreport-352"></a><br><dt>`<samp><span class="samp">-p, --perf-report, </span><code>%option perf-report</code></samp>'<dd>generates a performance report to <samp><span class="file">stderr</span></samp>. The report consists of
comments regarding features of the <code>flex</code> input file which will
cause a serious loss of performance in the resulting scanner. If you
give the flag twice, you will also get comments regarding features that
lead to minor performance losses.
<p>Note that the use of <code>REJECT</code>, and
variable trailing context (see <a href="#Limitations">Limitations</a>) entails a substantial
performance penalty; use of <code>yymore()</code>, the `<samp><span class="samp">^</span></samp>' operator, and
the `<samp><span class="samp">--interactive</span></samp>' flag entail minor performance penalties.
<p><a name="option_002dnodefault"></a>
<a name="index-g_t_002ds-353"></a><a name="index-g_t_002d_002d_002dnodefault-354"></a><a name="index-nodefault-355"></a><br><dt>`<samp><span class="samp">-s, --nodefault, </span><code>%option nodefault</code></samp>'<dd>causes the <em>default rule</em> (that unmatched scanner input is echoed
to <samp><span class="file">stdout)</span></samp> to be suppressed. If the scanner encounters input
that does not match any of its rules, it aborts with an error. This
option is useful for finding holes in a scanner's rule set.
<p><a name="option_002dtrace"></a>
<a name="index-g_t_002dT-356"></a><a name="index-g_t_002d_002d_002dtrace-357"></a><a name="index-trace-358"></a><br><dt>`<samp><span class="samp">-T, --trace, </span><code>%option trace</code></samp>'<dd>makes <code>flex</code> run in <dfn>trace</dfn> mode. It will generate a lot of
messages to <samp><span class="file">stderr</span></samp> concerning the form of the input and the
resultant non-deterministic and deterministic finite automata. This
option is mostly for use in maintaining <code>flex</code>.
<a name="index-g_t_002dv-362"></a><a name="index-g_t_002d_002d_002dverbose-363"></a><a name="index-verbose-364"></a><br><dt>`<samp><span class="samp">-v, --verbose, </span><code>%option verbose</code></samp>'<dd>specifies that <code>flex</code> should write to <samp><span class="file">stderr</span></samp> a summary of
statistics regarding the scanner it generates. Most of the statistics
are meaningless to the casual <code>flex</code> user, but the first line
identifies the version of <code>flex</code> (same as reported by `<samp><span class="samp">--version</span></samp>'),
and the next line the flags used when generating the scanner, including
those that are on by default.
<p><a name="option_002dwarn"></a>
<a name="index-g_t_002d_002d_002dwarn-365"></a><a name="index-warn-366"></a><br><dt>`<samp><span class="samp">--warn, </span><code>%option warn</code></samp>'<dd>warn about certain things. In particular, if the default rule can be
matched but no defualt rule has been given, the flex will warn you.
<br><dt>`<samp><span class="samp">-h, -?, --help</span></samp>'<dd>generates a “help” summary of <code>flex</code>'s options to <samp><span class="file">stdout</span></samp>
and then exits.
<p><a name="index-g_t_002dn-370"></a><br><dt>`<samp><span class="samp">-n</span></samp>'<dd>is another do-nothing option included only for
POSIX compliance.
<p><a name="index-g_t_002dV-371"></a><a name="index-g_t_002d_002d_002dversion-372"></a><br><dt>`<samp><span class="samp">-V, --version</span></samp>'<dd>prints the version number to <samp><span class="file">stdout</span></samp> and exits.
<p>with the first two all being quite expensive and the last two being
quite cheap. Note also that <code>unput()</code> is implemented as a routine
call that potentially does quite a bit of work, while <code>yyless()</code> is
a quite-cheap macro. So if you are just putting back some excess text
you scanned, use <code>ss()</code>.
<p><code>REJECT</code> should be avoided at all costs when performance is
important. It is a particularly expensive option.
<p>There is one case when <code>%option yylineno</code> can be expensive. That is when
your patterns match long tokens that could <em>possibly</em> contain a newline
character. There is no performance penalty for rules that can not possibly
match newlines, since flex does not need to check them for newlines. In
general, you should avoid rules such as <code>[^f]+</code>, which match very long
tokens, including newlines, and may possibly match your entire file! A better
approach is to separate <code>[^f]+</code> into two rules:
<pre class="example"><pre class="verbatim">
%option yylineno
%%
[^f\n]+
\n+
</pre>
</pre>
<p>The above scanner does not incur a performance penalty.
<p><a name="index-patterns_002c-tuning-for-performance-377"></a><a name="index-performance_002c-backing-up-378"></a><a name="index-backing-up_002c-example-of-eliminating-379"></a>Getting rid of backing up is messy and often may be an enormous amount
of work for a complicated scanner. In principal, one begins by using
the `<samp><span class="samp">-b</span></samp>' flag to generate a <samp><span class="file">lex.backup</span></samp> file. For example,
<p>Now instead of each newline requiring the processing of another action,
recognizing the newlines is distributed over the other rules to keep the
matched text as long as possible. Note that <em>adding</em> rules does
<em>not</em> slow down the scanner! The speed of the scanner is
independent of the number of rules or (modulo the considerations given
at the beginning of this section) how complicated the rules are with
regard to operators such as `<samp><span class="samp">*</span></samp>' and `<samp><span class="samp">|</span></samp>'.
<p><a name="index-keywords_002c-for-performance-386"></a><a name="index-performance_002c-using-keywords-387"></a>A final example in speeding up a scanner: suppose you want to scan
through a file containing identifiers and keywords, one per line
and with no other extraneous characters, and recognize all the
<h2 class="chapter">18 Generating C++ Scanners</h2>
<p><a name="index-c_002b_002b_002c-experimental-form-of-scanner-class-389"></a><a name="index-experimental-form-of-c_002b_002b-scanner-class-390"></a><strong>IMPORTANT</strong>: the present form of the scanning class is <em>experimental</em>
and may change considerably between major releases.
<p><a name="index-C_002b_002b-391"></a><a name="index-member-functions_002c-C_002b_002b-392"></a><a name="index-methods_002c-c_002b_002b-393"></a><code>flex</code> provides two different ways to generate scanners for use
with C++. The first way is to simply compile a scanner generated by
<code>flex</code> using a C++ compiler instead of a C compiler. You should
not encounter any compilation errors (see <a href="#Reporting-Bugs">Reporting Bugs</a>). You can
then use C++ code in your rule actions instead of C code. Note that the
default input source for your scanner remains <samp><span class="file">yyin</span></samp>, and default
echoing is still done to <samp><span class="file">yyout</span></samp>. Both of these remain <code>FILE
*</code> variables and not C++ <em>streams</em>.
<p>You can also use <code>flex</code> to generate a C++ scanner class, using the
`<samp><span class="samp">-+</span></samp>' option (or, equivalently, <code>%option c++)</code>, which is
automatically specified if the name of the <code>flex</code> executable ends
in a '+', such as <code>flex++</code>. When using this option, <code>flex</code>
defaults to generating the scanner to the file <samp><span class="file">lex.yy.cc</span></samp> instead
of <samp><span class="file">lex.yy.c</span></samp>. The generated scanner includes the header file
<samp><span class="file">FlexLexer.h</span></samp>, which defines the interface to two C++ classes.
<p>The first class,
<code>FlexLexer</code>,
provides an abstract base class defining the general scanner class
interface. It provides the following member functions:
<dl><dt><code>const char* YYText()</code><dd>returns the text of the most recently matched token, the equivalent of
<code>yytext</code>.
<p><a name="index-YYLeng-_0028C_002b_002b-only_0029-395"></a><br><dt><code>int YYLeng()</code><dd>returns the length of the most recently matched token, the equivalent of
<code>yyleng</code>.
<p><a name="index-lineno-_0028C_002b_002b-only_0029-396"></a><br><dt><code>int lineno() const</code><dd>returns the current input line number (see <code>%option yylineno)</code>, or
<code>1</code> if <code>%option yylineno</code> was not used.
<p><a name="index-set_005fdebug-_0028C_002b_002b-only_0029-397"></a><br><dt><code>void set_debug( int flag )</code><dd>sets the debugging flag for the scanner, equivalent to assigning to
<code>yy_flex_debug</code> (see <a href="#Scanner-Options">Scanner Options</a>). Note that you must build
the scannerusing <code>%option debug</code> to include debugging information
in it.
<p><a name="index-debug-_0028C_002b_002b-only_0029-398"></a><br><dt><code>int debug() const</code><dd>returns the current setting of the debugging flag.
</dl>
<p>Also provided are member functions equivalent to
<code>yy_switch_to_buffer()</code>, <code>yy_create_buffer()</code> (though the
first argument is an <code>istream*</code> object pointer and not a
<code>FILE*)</code>, <code>yy_flush_buffer()</code>, <code>yy_delete_buffer()</code>, and
<code>yyrestart()</code> (again, the first argument is a <code>istream*</code>
object pointer).
<p><a name="index-yyFlexLexer-_0028C_002b_002b-only_0029-399"></a><a name="index-FlexLexer-_0028C_002b_002b-only_0029-400"></a>The second class defined in <samp><span class="file">FlexLexer.h</span></samp> is <code>yyFlexLexer</code>,
which is derived from <code>FlexLexer</code>. It defines the following
<dl><dt><code>yyFlexLexer( istream* arg_yyin = 0, ostream* arg_yyout = 0 )</code><dd>constructs a <code>yyFlexLexer</code> object using the given streams for input
and output. If not specified, the streams default to <code>cin</code> and
<code>cout</code>, respectively.
<p><a name="index-yylex-_0028C_002b_002b-version_0029-402"></a><br><dt><code>virtual int yylex()</code><dd>performs the same role is <code>yylex()</code> does for ordinary <code>flex</code>
scanners: it scans the input stream, consuming tokens, until a rule's
action returns a value. If you derive a subclass <code>S</code> from
<code>yyFlexLexer</code> and want to access the member functions and variables
of <code>S</code> inside <code>yylex()</code>, then you need to use <code>%option
yyclass="S"</code> to inform <code>flex</code> that you will be using that subclass
instead of <code>yyFlexLexer</code>. In this case, rather than generating
<dl><dt><code>virtual int LexerInput( char* buf, int max_size )</code><dd>reads up to <code>max_size</code> characters into <code>buf</code> and returns the
number of characters read. To indicate end-of-input, return 0
characters. Note that <code>interactive</code> scanners (see the `<samp><span class="samp">-B</span></samp>'
and `<samp><span class="samp">-I</span></samp>' flags in <a href="#Scanner-Options">Scanner Options</a>) define the macro
<code>YY_INTERACTIVE</code>. If you redefine <code>LexerInput()</code> and need to
take different actions depending on whether or not the scanner might be
scanning an interactive input source, you can test for the presence of
this name via <code>#ifdef</code> statements.
<p><a name="index-LexerOutput-_0028C_002b_002b-only_0029-405"></a><br><dt><code>virtual void LexerOutput( const char* buf, int size )</code><dd>writes out <code>size</code> characters from the buffer <code>buf</code>, which, while
<code>NUL</code>-terminated, may also contain internal <code>NUL</code>s if the
scanner's rules can match text with <code>NUL</code>s in them.
<p><a name="index-error-reporting_002c-in-C_002b_002b-406"></a><a name="index-LexerError-_0028C_002b_002b-only_0029-407"></a><br><dt><code>virtual void LexerError( const char* msg )</code><dd>reports a fatal error message. The default version of this function
writes the message to the stream <code>cerr</code> and exits.
</dl>
<p>Note that a <code>yyFlexLexer</code> object contains its <em>entire</em>
scanning state. Thus you can use such objects to create reentrant
scanners, but see also <a href="#Reentrant">Reentrant</a>. You can instantiate multiple
instances of the same <code>yyFlexLexer</code> class, and you can also combine
multiple C++ scanner classes together in the same program using the
<h4 class="subsection">19.4.2 The Extra Argument</h4>
<p><a name="index-reentrant_002c-calling-functions-418"></a><a name="index-yyscanner-_0028reentrant-only_0029-419"></a>All functions take one additional argument: <code>yyscanner</code>.
<p>Notice that the calls to <code>yy_push_state</code> and <code>yy_pop_state</code>
both have an argument, <code>yyscanner</code> , that is not present in a
non-reentrant scanner. Here are the declarations of
<code>yy_push_state</code> and <code>yy_pop_state</code> in the generated scanner:
<p><code>flex</code> does not support this feature.
<li>The <code>lex</code> <code>%r</code> (generate a Ratfor scanner) option is not
supported. It is not part of the POSIX specification.
<li>After a call to <code>unput()</code>, <em>yytext</em> is undefined until the
next token is matched, unless the scanner was built using <code>%array</code>.
This is not the case with <code>lex</code> or the POSIX specification. The
`<samp><span class="samp">-l</span></samp>' option does away with this incompatibility.
<li>The precedence of the `<samp><span class="samp">{,}</span></samp>' (numeric range) operator is
different. The AT&T and POSIX specifications of <code>lex</code>
interpret `<samp><span class="samp">abc{1,3}</span></samp>' as match one, two,
or three occurrences of `<samp><span class="samp">abc</span></samp>'”, whereas <code>flex</code> interprets it
as “match `<samp><span class="samp">ab</span></samp>' followed by one, two, or three occurrences of
`<samp><span class="samp">c</span></samp>'”. The `<samp><span class="samp">-l</span></samp>' and `<samp><span class="samp">--posix</span></samp>' options do away with this
incompatibility.
<li>The precedence of the `<samp><span class="samp">^</span></samp>' operator is different. <code>lex</code>
interprets `<samp><span class="samp">^foo|bar</span></samp>' as “match either 'foo' at the beginning of a
line, or 'bar' anywhere”, whereas <code>flex</code> interprets it as “match
either `<samp><span class="samp">foo</span></samp>' or `<samp><span class="samp">bar</span></samp>' if they come at the beginning of a
line”. The latter is in agreement with the POSIX specification.
<li>The special table-size declarations such as <code>%a</code> supported by
<code>lex</code> are not required by <code>flex</code> scanners.. <code>flex</code>
ignores them.
<li>The name <code>FLEX_SCANNER</code> is <code>#define</code>'d so scanners may be
written for use with either <code>flex</code> or <code>lex</code>. Scanners also
include <code>YY_FLEX_MAJOR_VERSION</code>, <code>YY_FLEX_MINOR_VERSION</code>
and <code>YY_FLEX_SUBMINOR_VERSION</code>
indicating which version of <code>flex</code> generated the scanner. For
example, for the 2.5.22 release, these defines would be 2, 5 and 22
respectively. If the version of <code>flex</code> being used is a beta
version, then the symbol <code>FLEX_BETA</code> is defined.
</ul>
<p><a name="index-POSIX-comp_003bcompliance-453"></a><a name="index-non_002dPOSIX-features-of-flex-454"></a>The following <code>flex</code> features are not included in <code>lex</code> or the
POSIX specification:
<ul>
<li>C++ scanners
<li>%option
<li>start condition scopes
<li>start condition stacks
<li>interactive/non-interactive scanners
<li>yy_scan_string() and friends
<li>yyterminate()
<li>yy_set_interactive()
<li>yy_set_bol()
<li>YY_AT_BOL()
<<EOF>>
<li><*>
<li>YY_DECL
<li>YY_START
<li>YY_USER_ACTION
<li>YY_USER_INIT
<li>#line directives
<li>%{}'s around actions
<li>reentrant C API
<li>multiple actions on a line
<li>almost all of the <code>flex</code> command-line options
</ul>
<p>The feature “multiple actions on a line”
refers to the fact that with <code>flex</code> you can put multiple actions on
the same line, separated with semi-colons, while with <code>lex</code>, the
following:
<pre class="example"><pre class="verbatim">
foo handle_foo(); ++num_foos_seen;
</pre>
</pre>
<p>is (rather surprisingly) truncated to
<pre class="example"><pre class="verbatim">
foo handle_foo();
</pre>
</pre>
<p><code>flex</code> does not truncate the action. Actions that are not enclosed
in braces are simply terminated at the end of the line.
<h3 class="section">21.1 The Default Memory Management</h3>
<p>Flex allocates dynamic memory during initialization, and once in a while from
within a call to yylex(). Initialization takes place during the first call to
yylex(). Thereafter, flex may reallocate more memory if it needs to enlarge a
buffer. As of version 2.5.9 Flex will clean up all memory when you call <code>yylex_destroy</code>
See <a href="#faq_002dmemory_002dleak">faq-memory-leak</a>.
<p>Flex allocates dynamic memory for four purposes, listed below <a rel="footnote" href="#fn-2" name="fnd-2"><sup>2</sup></a>
<dl>
<dt>16kB for the input buffer.<dd>Flex allocates memory for the character buffer used to perform pattern
matching. Flex must read ahead from the input stream and store it in a large
character buffer. This buffer is typically the largest chunk of dynamic memory
flex consumes. This buffer will grow if necessary, doubling the size each time.
Flex frees this memory when you call yylex_destroy(). The default size of this
buffer (16384 bytes) is almost always too large. The ideal size for this
buffer is the length of the longest token expected. Flex will allocate a few
extra bytes for housekeeping.
<br><dt>16kb for the REJECT state. This will only be allocated if you use REJECT.<dd>The size is the same as the input buffer, so if you override the size of the
input buffer, then you automatically override the size of this buffer as well.
<br><dt>100 bytes for the start condition stack.<dd>Flex allocates memory for the start condition stack. This is the stack used
for pushing start states, i.e., with yy_push_state(). It will grow if
necessary. Since the states are simply integers, this stack doesn't consume
much memory. This stack is not present if <code>%option stack</code> is not
specified. You will rarely need to tune this buffer. The ideal size for this
stack is the maximum depth expected. The memory for this stack is
automatically destroyed when you call yylex_destroy(). See <a href="#option_002dstack">option-stack</a>.
<br><dt>40 bytes for each YY_BUFFER_STATE.<dd>Flex allocates memory for each YY_BUFFER_STATE. The buffer state itself
is about 40 bytes, plus an additional large character buffer (described above.)
The initial buffer state is created during initialization, and with each call
to yy_create_buffer(). You can't tune the size of this, but you can tune the
character buffer as described above. Any buffer state that you explicitly
create by calling yy_create_buffer() is <em>NOT</em> destroyed automatically. You
must call yy_delete_buffer() to free the memory. The exception to this rule is
that flex will delete the current buffer automatically when you call
yylex_destroy(). If you delete the current buffer, be sure to set it to NULL.
That way, flex will not try to delete the buffer a second time (possibly
crashing your program!) At the time of this writing, flex does not provide a
growable stack for the buffer states. You have to manage that yourself.
See <a href="#Multiple-Input-Buffers">Multiple Input Buffers</a>.
<br><dt>84 bytes for the reentrant scanner guts<dd>Flex allocates about 84 bytes for the reentrant scanner structure when
you call yylex_init(). It is destroyed when the user calls yylex_destroy().
<p>From time to time, the <code>flex</code> maintainer receives certain
questions. Rather than repeat answers to well-understood problems, we
publish them here.
<ul class="menu">
<li><a accesskey="1" href="#When-was-flex-born_003f">When was flex born?</a>
<li><a accesskey="2" href="#How-do-I-expand-_005c-escape-sequences-in-C_002dstyle-quoted-strings_003f">How do I expand \ escape sequences in C-style quoted strings?</a>
<li><a accesskey="3" href="#Why-do-flex-scanners-call-fileno-if-it-is-not-ANSI-compatible_003f">Why do flex scanners call fileno if it is not ANSI compatible?</a>
<li><a accesskey="4" href="#Does-flex-support-recursive-pattern-definitions_003f">Does flex support recursive pattern definitions?</a>
<li><a accesskey="5" href="#How-do-I-skip-huge-chunks-of-input-_0028tens-of-megabytes_0029-while-using-flex_003f">How do I skip huge chunks of input (tens of megabytes) while using flex?</a>
<li><a accesskey="6" href="#Flex-is-not-matching-my-patterns-in-the-same-order-that-I-defined-them_002e">Flex is not matching my patterns in the same order that I defined them.</a>
<li><a accesskey="7" href="#My-actions-are-executing-out-of-order-or-sometimes-not-at-all_002e">My actions are executing out of order or sometimes not at all.</a>
<li><a accesskey="8" href="#How-can-I-have-multiple-input-sources-feed-into-the-same-scanner-at-the-same-time_003f">How can I have multiple input sources feed into the same scanner at the same time?</a>
<li><a accesskey="9" href="#Can-I-build-nested-parsers-that-work-with-the-same-input-file_003f">Can I build nested parsers that work with the same input file?</a>
<li><a href="#How-can-I-match-text-only-at-the-end-of-a-file_003f">How can I match text only at the end of a file?</a>
<li><a href="#How-can-I-make-REJECT-cascade-across-start-condition-boundaries_003f">How can I make REJECT cascade across start condition boundaries?</a>
<li><a href="#Why-cant-I-use-fast-or-full-tables-with-interactive-mode_003f">Why cant I use fast or full tables with interactive mode?</a>
<li><a href="#How-much-faster-is-_002dF-or-_002df-than-_002dC_003f">How much faster is -F or -f than -C?</a>
<li><a href="#If-I-have-a-simple-grammar-cant-I-just-parse-it-with-flex_003f">If I have a simple grammar cant I just parse it with flex?</a>
<li><a href="#Why-doesnt-yyrestart_0028_0029-set-the-start-state-back-to-INITIAL_003f">Why doesnt yyrestart() set the start state back to INITIAL?</a>
<li><a href="#How-can-I-match-C_002dstyle-comments_003f">How can I match C-style comments?</a>
<li><a href="#The-period-isnt-working-the-way-I-expected_002e">The period isnt working the way I expected.</a>
<li><a href="#Can-I-get-the-flex-manual-in-another-format_003f">Can I get the flex manual in another format?</a>
<li><a href="#Does-there-exist-a-_0022faster_0022-NDFA_002d_003eDFA-algorithm_003f">Does there exist a "faster" NDFA->DFA algorithm?</a>
<li><a href="#How-does-flex-compile-the-DFA-so-quickly_003f">How does flex compile the DFA so quickly?</a>
<li><a href="#How-can-I-use-more-than-8192-rules_003f">How can I use more than 8192 rules?</a>
<li><a href="#How-do-I-abandon-a-file-in-the-middle-of-a-scan-and-switch-to-a-new-file_003f">How do I abandon a file in the middle of a scan and switch to a new file?</a>
<li><a href="#How-do-I-execute-code-only-during-initialization-_0028only-before-the-first-scan_0029_003f">How do I execute code only during initialization (only before the first scan)?</a>
<li><a href="#How-do-I-execute-code-at-termination_003f">How do I execute code at termination?</a>
<li><a href="#Where-else-can-I-find-help_003f">Where else can I find help?</a>
<li><a href="#Can-I-include-comments-in-the-_0022rules_0022-section-of-the-file_003f">Can I include comments in the "rules" section of the file?</a>
<li><a href="#I-get-an-error-about-undefined-yywrap_0028_0029_002e">I get an error about undefined yywrap().</a>
<li><a href="#How-can-I-change-the-matching-pattern-at-run-time_003f">How can I change the matching pattern at run time?</a>
<li><a href="#How-can-I-expand-macros-in-the-input_003f">How can I expand macros in the input?</a>
<li><a href="#How-can-I-build-a-two_002dpass-scanner_003f">How can I build a two-pass scanner?</a>
<li><a href="#How-do-I-match-any-string-not-matched-in-the-preceding-rules_003f">How do I match any string not matched in the preceding rules?</a>
<li><a href="#I-am-trying-to-port-code-from-AT_0026T-lex-that-uses-yysptr-and-yysbuf_002e">I am trying to port code from AT&T lex that uses yysptr and yysbuf.</a>
<li><a href="#Is-there-a-way-to-make-flex-treat-NULL-like-a-regular-character_003f">Is there a way to make flex treat NULL like a regular character?</a>
<li><a href="#Whenever-flex-can-not-match-the-input-it-says-_0022flex-scanner-jammed_0022_002e">Whenever flex can not match the input it says "flex scanner jammed".</a>
<li><a href="#Why-doesnt-flex-have-non_002dgreedy-operators-like-perl-does_003f">Why doesnt flex have non-greedy operators like perl does?</a>
<li><a href="#Memory-leak-_002d-16386-bytes-allocated-by-malloc_002e">Memory leak - 16386 bytes allocated by malloc.</a>
<li><a href="#How-do-I-track-the-byte-offset-for-lseek_0028_0029_003f">How do I track the byte offset for lseek()?</a>
<li><a href="#How-do-I-use-my-own-I_002fO-classes-in-a-C_002b_002b-scanner_003f">How do I use my own I/O classes in a C++ scanner?</a>
<li><a href="#How-do-I-skip-as-many-chars-as-possible_003f">How do I skip as many chars as possible?</a>
<li><a href="#deleteme00">deleteme00</a>
<li><a href="#Are-certain-equivalent-patterns-faster-than-others_003f">Are certain equivalent patterns faster than others?</a>
<li><a href="#Is-backing-up-a-big-deal_003f">Is backing up a big deal?</a>
<li><a href="#Can-I-fake-multi_002dbyte-character-support_003f">Can I fake multi-byte character support?</a>
<li><a href="#deleteme01">deleteme01</a>
<li><a href="#Can-you-discuss-some-flex-internals_003f">Can you discuss some flex internals?</a>
<li><a href="#unput_0028_0029-messes-up-yy_005fat_005fbol">unput() messes up yy_at_bol</a>
<li><a href="#The-_007c-operator-is-not-doing-what-I-want">The | operator is not doing what I want</a>
<li><a href="#Why-can_0027t-flex-understand-this-variable-trailing-context-pattern_003f">Why can't flex understand this variable trailing context pattern?</a>
<li><a href="#Trailing-context-is-getting-confused-with-trailing-optional-patterns">Trailing context is getting confused with trailing optional patterns</a>
<li><a href="#Is-flex-GNU-or-not_003f">Is flex GNU or not?</a>
<li><a href="#ERASEME53">ERASEME53</a>
<li><a href="#I-need-to-scan-if_002dthen_002delse-blocks-and-while-loops">I need to scan if-then-else blocks and while loops</a>
<li><a href="#ERASEME55">ERASEME55</a>
<li><a href="#ERASEME56">ERASEME56</a>
<li><a href="#ERASEME57">ERASEME57</a>
<li><a href="#Is-there-a-repository-for-flex-scanners_003f">Is there a repository for flex scanners?</a>
<li><a href="#How-can-I-conditionally-compile-or-preprocess-my-flex-input-file_003f">How can I conditionally compile or preprocess my flex input file?</a>
<li><a href="#Where-can-I-find-grammars-for-lex-and-yacc_003f">Where can I find grammars for lex and yacc?</a>
<li><a href="#I-get-an-end_002dof_002dbuffer-message-for-each-character-scanned_002e">I get an end-of-buffer message for each character scanned.</a>
Next: <a rel="next" accesskey="n" href="#How-do-I-expand-_005c-escape-sequences-in-C_002dstyle-quoted-strings_003f">How do I expand \ escape sequences in C-style quoted strings?</a>,
Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
</div>
<h3 class="unnumberedsec">When was flex born?</h3>
<p>Vern Paxson took over
the <cite>Software Tools</cite> lex project from Jef Poskanzer in 1982. At that point it
was written in Ratfor. Around 1987 or so, Paxson translated it into C, and
Next: <a rel="next" accesskey="n" href="#Why-do-flex-scanners-call-fileno-if-it-is-not-ANSI-compatible_003f">Why do flex scanners call fileno if it is not ANSI compatible?</a>,
Previous: <a rel="previous" accesskey="p" href="#When-was-flex-born_003f">When was flex born?</a>,
Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
</div>
<h3 class="unnumberedsec">How do I expand \ escape sequences in C-style quoted strings?</h3>
<p>A key point when scanning quoted strings is that you cannot (easily) write
a single rule that will precisely match the string if you allow things
like embedded escape sequences and newlines. If you try to match strings
with a single rule then you'll wind up having to rescan the string anyway
to find any escape sequences.
<p>Instead you can use exclusive start conditions and a set of rules, one for
matching non-escaped text, one for matching a single escape, one for
matching an embedded newline, and one for recognizing the end of the
string. Each of these rules is then faced with the question of where to
put its intermediary results. The best solution is for the rules to
append their local value of <code>yytext</code> to the end of a “string literal”
buffer. A rule like the escape-matcher will append to the buffer the
meaning of the escape sequence rather than the literal text in <code>yytext</code>.
In this way, <code>yytext</code> does not need to be modified at all.
Next: <a rel="next" accesskey="n" href="#Does-flex-support-recursive-pattern-definitions_003f">Does flex support recursive pattern definitions?</a>,
Previous: <a rel="previous" accesskey="p" href="#How-do-I-expand-_005c-escape-sequences-in-C_002dstyle-quoted-strings_003f">How do I expand \ escape sequences in C-style quoted strings?</a>,
Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
</div>
<h3 class="unnumberedsec">Why do flex scanners call fileno if it is not ANSI compatible?</h3>
<p>Flex scanners call <code>fileno()</code> in order to get the file descriptor
corresponding to <code>yyin</code>. The file descriptor may be passed to
<code>isatty()</code> or <code>read()</code>, depending upon which <code>%options</code> you specified.
If your system does not have <code>fileno()</code> support, to get rid of the
<code>read()</code> call, do not specify <code>%option read</code>. To get rid of the <code>isatty()</code>
call, you must specify one of <code>%option always-interactive</code> or
Next: <a rel="next" accesskey="n" href="#How-do-I-skip-huge-chunks-of-input-_0028tens-of-megabytes_0029-while-using-flex_003f">How do I skip huge chunks of input (tens of megabytes) while using flex?</a>,
Previous: <a rel="previous" accesskey="p" href="#Why-do-flex-scanners-call-fileno-if-it-is-not-ANSI-compatible_003f">Why do flex scanners call fileno if it is not ANSI compatible?</a>,
Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
</div>
<h3 class="unnumberedsec">Does flex support recursive pattern definitions?</h3>
<p>e.g.,
<pre class="example"><pre class="verbatim">
%%
block "{"({block}|{statement})*"}"
</pre>
</pre>
<p>No. You cannot have recursive definitions. The pattern-matching power of
regular expressions in general (and therefore flex scanners, too) is
limited. In particular, regular expressions cannot “balance” parentheses
to an arbitrary degree. For example, it's impossible to write a regular
expression that matches all strings containing the same number of '{'s
as '}'s. For more powerful pattern matching, you need a parser, such
Next: <a rel="next" accesskey="n" href="#Flex-is-not-matching-my-patterns-in-the-same-order-that-I-defined-them_002e">Flex is not matching my patterns in the same order that I defined them.</a>,
Previous: <a rel="previous" accesskey="p" href="#Does-flex-support-recursive-pattern-definitions_003f">Does flex support recursive pattern definitions?</a>,
Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
</div>
<h3 class="unnumberedsec">How do I skip huge chunks of input (tens of megabytes) while using flex?</h3>
<p>Use <code>fseek()</code> (or <code>lseek()</code>) to position yyin, then call <code>yyrestart()</code>.
Next: <a rel="next" accesskey="n" href="#My-actions-are-executing-out-of-order-or-sometimes-not-at-all_002e">My actions are executing out of order or sometimes not at all.</a>,
Previous: <a rel="previous" accesskey="p" href="#How-do-I-skip-huge-chunks-of-input-_0028tens-of-megabytes_0029-while-using-flex_003f">How do I skip huge chunks of input (tens of megabytes) while using flex?</a>,
Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
</div>
<h3 class="unnumberedsec">Flex is not matching my patterns in the same order that I defined them.</h3>
<p><code>flex</code> picks the
rule that matches the most text (i.e., the longest possible input string).
This is because <code>flex</code> uses an entirely different matching technique
(“deterministic finite automata”) that actually does all of the matching
simultaneously, in parallel. (Seems impossible, but it's actually a fairly
simple technique once you understand the principles.)
<p>A side-effect of this parallel matching is that when the input matches more
than one rule, <code>flex</code> scanners pick the rule that matched the <em>most</em> text. This
is explained further in the manual, in the section See <a href="#Matching">Matching</a>.
<p>If you want <code>flex</code> to choose a shorter match, then you can work around this
behavior by expanding your short
rule to match more text, then put back the extra:
<pre class="example"><pre class="verbatim">
data_.* yyless( 5 ); BEGIN BLOCKIDSTATE;
</pre>
</pre>
<p>Another fix would be to make the second rule active only during the
<code><BLOCKIDSTATE></code> start condition, and make that start condition exclusive
by declaring it with <code>%x</code> instead of <code>%s</code>.
<p>A final fix is to change the input language so that the ambiguity for
`<samp><span class="samp">data_</span></samp>' is removed, by adding characters to it that don't match the
identifier rule, or by removing characters (such as `<samp><span class="samp">_</span></samp>') from the
identifier rule so it no longer matches `<samp><span class="samp">data_</span></samp>'. (Of course, you might
also not have the option of changing the input language.)
Next: <a rel="next" accesskey="n" href="#How-can-I-have-multiple-input-sources-feed-into-the-same-scanner-at-the-same-time_003f">How can I have multiple input sources feed into the same scanner at the same time?</a>,
Previous: <a rel="previous" accesskey="p" href="#Flex-is-not-matching-my-patterns-in-the-same-order-that-I-defined-them_002e">Flex is not matching my patterns in the same order that I defined them.</a>,
Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
</div>
<h3 class="unnumberedsec">My actions are executing out of order or sometimes not at all.</h3>
<p>Most likely, you have (in error) placed the opening `<samp><span class="samp">{</span></samp>' of the action
block on a different line than the rule, e.g.,
<pre class="example"><pre class="verbatim">
^(foo|bar)
{ <<<--- WRONG!
}
</pre>
</pre>
<p><code>flex</code> requires that the opening `<samp><span class="samp">{</span></samp>' of an action associated with a rule
begin on the same line as does the rule. You need instead to write your rules
Next: <a rel="next" accesskey="n" href="#Can-I-build-nested-parsers-that-work-with-the-same-input-file_003f">Can I build nested parsers that work with the same input file?</a>,
Previous: <a rel="previous" accesskey="p" href="#My-actions-are-executing-out-of-order-or-sometimes-not-at-all_002e">My actions are executing out of order or sometimes not at all.</a>,
Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
</div>
<h3 class="unnumberedsec">How can I have multiple input sources feed into the same scanner at the same time?</h3>
<p>If <small class="dots">...</small>
<ul>
<li>your scanner is free of backtracking (verified using <code>flex</code>'s `<samp><span class="samp">-b</span></samp>' flag),
<li>AND you run your scanner interactively (`<samp><span class="samp">-I</span></samp>' option; default unless using special table
compression options),
<li>AND you feed it one character at a time by redefining <code>YY_INPUT</code> to do so,
</ul>
<p>then every time it matches a token, it will have exhausted its input
buffer (because the scanner is free of backtracking). This means you
can safely use <code>select()</code> at the point and only call <code>yylex()</code> for another
token if <code>select()</code> indicates there's data available.
<p>That is, move the <code>select()</code> out from the input function to a point where
it determines whether <code>yylex()</code> gets called for the next token.
<p>With this approach, you will still have problems if your input can arrive
piecemeal; <code>select()</code> could inform you that the beginning of a token is
available, you call <code>yylex()</code> to get it, but it winds up blocking waiting
for the later characters in the token.
<p>Here's another way: Move your input multiplexing inside of <code>YY_INPUT</code>. That
is, whenever <code>YY_INPUT</code> is called, it <code>select()</code>'s to see where input is
available. If input is available for the scanner, it reads and returns the
next byte. If input is available from another source, it calls whatever
function is responsible for reading from that source. (If no input is
available, it blocks until some input is available.) I've used this technique in an
interpreter I wrote that both reads keyboard input using a <code>flex</code> scanner and
Next: <a rel="next" accesskey="n" href="#How-can-I-match-text-only-at-the-end-of-a-file_003f">How can I match text only at the end of a file?</a>,
Previous: <a rel="previous" accesskey="p" href="#How-can-I-have-multiple-input-sources-feed-into-the-same-scanner-at-the-same-time_003f">How can I have multiple input sources feed into the same scanner at the same time?</a>,
Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
</div>
<h3 class="unnumberedsec">Can I build nested parsers that work with the same input file?</h3>
<p>This is not going to work without some additional effort. The reason is
that <code>flex</code> block-buffers the input it reads from <code>yyin</code>. This means that the
“outermost” <code>yylex()</code>, when called, will automatically slurp up the first 8K
of input available on yyin, and subsequent calls to other <code>yylex()</code>'s won't
see that input. You might be tempted to work around this problem by
redefining <code>YY_INPUT</code> to only return a small amount of text, but it turns out
that that approach is quite difficult. Instead, the best solution is to
combine all of your scanners into one large scanner, using a different
Next: <a rel="next" accesskey="n" href="#How-can-I-make-REJECT-cascade-across-start-condition-boundaries_003f">How can I make REJECT cascade across start condition boundaries?</a>,
Previous: <a rel="previous" accesskey="p" href="#Can-I-build-nested-parsers-that-work-with-the-same-input-file_003f">Can I build nested parsers that work with the same input file?</a>,
Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
</div>
<h3 class="unnumberedsec">How can I match text only at the end of a file?</h3>
<p>There is no way to write a rule which is “match this text, but only if
it comes at the end of the file”. You can fake it, though, if you happen
to have a character lying around that you don't allow in your input.
Then you redefine <code>YY_INPUT</code> to call your own routine which, if it sees
an `<samp><span class="samp">EOF</span></samp>', returns the magic character first (and remembers to return a
real <code>EOF</code> next time it's called). Then you could write:
<pre class="example"><pre class="verbatim">
<COMMENT>(.|\n)*{EOF_CHAR} /* saw comment at EOF */
Next: <a rel="next" accesskey="n" href="#Why-cant-I-use-fast-or-full-tables-with-interactive-mode_003f">Why cant I use fast or full tables with interactive mode?</a>,
Previous: <a rel="previous" accesskey="p" href="#How-can-I-match-text-only-at-the-end-of-a-file_003f">How can I match text only at the end of a file?</a>,
Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
</div>
<h3 class="unnumberedsec">How can I make REJECT cascade across start condition boundaries?</h3>
<p>You can do this as follows. Suppose you have a start condition `<samp><span class="samp">A</span></samp>', and
after exhausting all of the possible matches in `<samp><span class="samp"><A></span></samp>', you want to try
matches in `<samp><span class="samp"><INITIAL></span></samp>'. Then you could use the following:
Next: <a rel="next" accesskey="n" href="#How-much-faster-is-_002dF-or-_002df-than-_002dC_003f">How much faster is -F or -f than -C?</a>,
Previous: <a rel="previous" accesskey="p" href="#How-can-I-make-REJECT-cascade-across-start-condition-boundaries_003f">How can I make REJECT cascade across start condition boundaries?</a>,
Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
</div>
<h3 class="unnumberedsec">Why can't I use fast or full tables with interactive mode?</h3>
<p>One of the assumptions
flex makes is that interactive applications are inherently slow (they're
waiting on a human after all).
It has to do with how the scanner detects that it must be finished scanning
a token. For interactive scanners, after scanning each character the current
state is looked up in a table (essentially) to see whether there's a chance
of another input character possibly extending the length of the match. If
not, the scanner halts. For non-interactive scanners, the end-of-token test
is much simpler, basically a compare with 0, so no memory bus cycles. Since
the test occurs in the innermost scanning loop, one would like to make it go
as fast as possible.
<p>Still, it seems reasonable to allow the user to choose to trade off a bit
of performance in this area to gain the corresponding flexibility. There
might be another reason, though, why fast scanners don't support the
Next: <a rel="next" accesskey="n" href="#If-I-have-a-simple-grammar-cant-I-just-parse-it-with-flex_003f">If I have a simple grammar cant I just parse it with flex?</a>,
Previous: <a rel="previous" accesskey="p" href="#Why-cant-I-use-fast-or-full-tables-with-interactive-mode_003f">Why cant I use fast or full tables with interactive mode?</a>,
Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
</div>
<h3 class="unnumberedsec">How much faster is -F or -f than -C?</h3>
Next: <a rel="next" accesskey="n" href="#Why-doesnt-yyrestart_0028_0029-set-the-start-state-back-to-INITIAL_003f">Why doesnt yyrestart() set the start state back to INITIAL?</a>,
Previous: <a rel="previous" accesskey="p" href="#How-much-faster-is-_002dF-or-_002df-than-_002dC_003f">How much faster is -F or -f than -C?</a>,
Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
</div>
<h3 class="unnumberedsec">If I have a simple grammar can't I just parse it with flex?</h3>
<p>Is your grammar recursive? That's almost always a sign that you're
better off using a parser/scanner rather than just trying to use a scanner
Next: <a rel="next" accesskey="n" href="#How-can-I-match-C_002dstyle-comments_003f">How can I match C-style comments?</a>,
Previous: <a rel="previous" accesskey="p" href="#If-I-have-a-simple-grammar-cant-I-just-parse-it-with-flex_003f">If I have a simple grammar cant I just parse it with flex?</a>,
Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
</div>
<h3 class="unnumberedsec">Why doesn't yyrestart() set the start state back to INITIAL?</h3>
<p>There are two reasons. The first is that there might
be programs that rely on the start state not changing across file changes.
The second is that beginning with <code>flex</code> version 2.4, use of <code>yyrestart()</code> is no longer required,
so fixing the problem there doesn't solve the more general problem.
Next: <a rel="next" accesskey="n" href="#The-period-isnt-working-the-way-I-expected_002e">The period isnt working the way I expected.</a>,
Previous: <a rel="previous" accesskey="p" href="#Why-doesnt-yyrestart_0028_0029-set-the-start-state-back-to-INITIAL_003f">Why doesnt yyrestart() set the start state back to INITIAL?</a>,
Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
</div>
<h3 class="unnumberedsec">How can I match C-style comments?</h3>
<p>You might be tempted to try something like this:
<pre class="example"><pre class="verbatim">
"/*".*"*/" // WRONG!
</pre>
</pre>
<p>or, worse, this:
<pre class="example"><pre class="verbatim">
"/*"(.|\n)"*/" // WRONG!
</pre>
</pre>
<p>The above rules will eat too much input, and blow up on things like:
<pre class="example"><pre class="verbatim">
/* a comment */ do_my_thing( "oops */" );
</pre>
</pre>
<p>Here is one way which allows you to track line information:
Next: <a rel="next" accesskey="n" href="#Can-I-get-the-flex-manual-in-another-format_003f">Can I get the flex manual in another format?</a>,
Previous: <a rel="previous" accesskey="p" href="#How-can-I-match-C_002dstyle-comments_003f">How can I match C-style comments?</a>,
Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
</div>
<h3 class="unnumberedsec">The '.' isn't working the way I expected.</h3>
<p>Here are some tips for using `<samp><span class="samp">.</span></samp>':
<ul>
<li>A common mistake is to place the grouping parenthesis AFTER an operator, when
you really meant to place the parenthesis BEFORE the operator, e.g., you
probably want this <code>(foo|bar)+</code> and NOT this <code>(foo|bar+)</code>.
<p>The first pattern matches the words `<samp><span class="samp">foo</span></samp>' or `<samp><span class="samp">bar</span></samp>' any number of
times, e.g., it matches the text `<samp><span class="samp">barfoofoobarfoo</span></samp>'. The
second pattern matches a single instance of <code>foo</code> or a single instance of
<code>bar</code> followed by one or more `<samp><span class="samp">r</span></samp>'s, e.g., it matches the text <code>barrrr</code> .
<li>A `<samp><span class="samp">.</span></samp>' inside `<samp><span class="samp">[]</span></samp>''s just means a literal`<samp><span class="samp">.</span></samp>' (period),
and NOT “any character except newline”.
<li>Remember that `<samp><span class="samp">.</span></samp>' matches any character EXCEPT `<samp><span class="samp">\n</span></samp>' (and `<samp><span class="samp">EOF</span></samp>').
If you really want to match ANY character, including newlines, then use <code>(.|\n)</code>
Beware that the regex <code>(.|\n)+</code> will match your entire input!
<li>Finally, if you want to match a literal `<samp><span class="samp">.</span></samp>' (a period), then use `<samp><span class="samp">[.]</span></samp>' or `<samp><span class="samp">"."</span></samp>'
Next: <a rel="next" accesskey="n" href="#How-do-I-abandon-a-file-in-the-middle-of-a-scan-and-switch-to-a-new-file_003f">How do I abandon a file in the middle of a scan and switch to a new file?</a>,
Previous: <a rel="previous" accesskey="p" href="#How-does-flex-compile-the-DFA-so-quickly_003f">How does flex compile the DFA so quickly?</a>,
Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
</div>
<h3 class="unnumberedsec">How can I use more than 8192 rules?</h3>
<p><code>Flex</code> is compiled with an upper limit of 8192 rules per scanner.
If you need more than 8192 rules in your scanner, you'll have to recompile <code>flex</code>
with the following changes in <samp><span class="file">flexdef.h</span></samp>:
<pre class="example"><pre class="verbatim">
< #define YY_TRAILING_MASK 0x2000
< #define YY_TRAILING_HEAD_MASK 0x4000
--
> #define YY_TRAILING_MASK 0x20000000
> #define YY_TRAILING_HEAD_MASK 0x40000000
</pre>
</pre>
<p>This should work okay as long as your C compiler uses 32 bit integers.
But you might want to think about whether using such a huge number of rules
is the best way to solve your problem.
<p>The following may also be relevant:
<p>With luck, you should be able to increase the definitions in flexdef.h for:
<pre class="example"><pre class="verbatim">
#define JAMSTATE -32766 /* marks a reference to the state that always jams */
#define MAXIMUM_MNS 31999
#define BAD_SUBSCRIPT -32767
</pre>
</pre>
<p>recompile everything, and it'll all work. Flex only has these 16-bit-like
values built into it because a long time ago it was developed on a machine
with 16-bit ints. I've given this advice to others in the past but haven't
heard back from them whether it worked okay or not...
Next: <a rel="next" accesskey="n" href="#How-do-I-execute-code-only-during-initialization-_0028only-before-the-first-scan_0029_003f">How do I execute code only during initialization (only before the first scan)?</a>,
Previous: <a rel="previous" accesskey="p" href="#How-can-I-use-more-than-8192-rules_003f">How can I use more than 8192 rules?</a>,
Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
</div>
<h3 class="unnumberedsec">How do I abandon a file in the middle of a scan and switch to a new file?</h3>
<p>Just call <code>yyrestart(newfile)</code>. Be sure to reset the start state if you want a
“fresh start, since <code>yyrestart</code> does NOT reset the start state back to <code>INITIAL</code>.
Next: <a rel="next" accesskey="n" href="#How-do-I-execute-code-at-termination_003f">How do I execute code at termination?</a>,
Previous: <a rel="previous" accesskey="p" href="#How-do-I-abandon-a-file-in-the-middle-of-a-scan-and-switch-to-a-new-file_003f">How do I abandon a file in the middle of a scan and switch to a new file?</a>,
Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
</div>
<h3 class="unnumberedsec">How do I execute code only during initialization (only before the first scan)?</h3>
<p>You can specify an initial action by defining the macro <code>YY_USER_INIT</code> (though
note that <code>yyout</code> may not be available at the time this macro is executed). Or you
Next: <a rel="next" accesskey="n" href="#Where-else-can-I-find-help_003f">Where else can I find help?</a>,
Previous: <a rel="previous" accesskey="p" href="#How-do-I-execute-code-only-during-initialization-_0028only-before-the-first-scan_0029_003f">How do I execute code only during initialization (only before the first scan)?</a>,
Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
</div>
<h3 class="unnumberedsec">How do I execute code at termination?</h3>
<p>You can specify an action for the <code><<EOF>></code> rule.
<div class="node">
<p><hr>
<a name="Where-else-can-I-find-help%3f"></a>
<a name="Where-else-can-I-find-help_003f"></a>
Next: <a rel="next" accesskey="n" href="#Can-I-include-comments-in-the-_0022rules_0022-section-of-the-file_003f">Can I include comments in the "rules" section of the file?</a>,
Previous: <a rel="previous" accesskey="p" href="#How-do-I-execute-code-at-termination_003f">How do I execute code at termination?</a>,
Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
</div>
<h3 class="unnumberedsec">Where else can I find help?</h3>
<p>You can find the flex homepage on the web at
<a href="http://lex.sourceforge.net/">http://lex.sourceforge.net/</a>. See that page for details about flex
Next: <a rel="next" accesskey="n" href="#How-can-I-change-the-matching-pattern-at-run-time_003f">How can I change the matching pattern at run time?</a>,
Previous: <a rel="previous" accesskey="p" href="#Can-I-include-comments-in-the-_0022rules_0022-section-of-the-file_003f">Can I include comments in the "rules" section of the file?</a>,
Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
</div>
<h3 class="unnumberedsec">I get an error about undefined yywrap().</h3>
<p>You must supply a <code>yywrap()</code> function of your own, or link to <samp><span class="file">libfl.a</span></samp>
(which provides one), or use
<pre class="example"><pre class="verbatim">
%option noyywrap
</pre>
</pre>
<p>in your source to say you don't want a <code>yywrap()</code> function.
Next: <a rel="next" accesskey="n" href="#How-can-I-build-a-two_002dpass-scanner_003f">How can I build a two-pass scanner?</a>,
Previous: <a rel="previous" accesskey="p" href="#How-can-I-change-the-matching-pattern-at-run-time_003f">How can I change the matching pattern at run time?</a>,
Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
</div>
<h3 class="unnumberedsec">How can I expand macros in the input?</h3>
<p>The best way to approach this problem is at a higher level, e.g., in the parser.
<p>However, you can do this using multiple input buffers.
<pre class="example"><pre class="verbatim">
%%
macro/[a-z]+ {
/* Saw the macro "macro" followed by extra stuff. */
Next: <a rel="next" accesskey="n" href="#How-do-I-match-any-string-not-matched-in-the-preceding-rules_003f">How do I match any string not matched in the preceding rules?</a>,
Previous: <a rel="previous" accesskey="p" href="#How-can-I-expand-macros-in-the-input_003f">How can I expand macros in the input?</a>,
Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
</div>
<h3 class="unnumberedsec">How can I build a two-pass scanner?</h3>
<p>One way to do it is to filter the first pass to a temporary file,
then process the temporary file on the second pass. You will probably see a
performance hit, do to all the disk I/O.
<p>When you need to look ahead far forward like this, it almost always means
that the right solution is to build a parse tree of the entire input, then
walk it after the parse in order to generate the output. In a sense, this
is a two-pass approach, once through the text and once through the parse
tree, but the performance hit for the latter is usually an order of magnitude
smaller, since everything is already classified, in binary format, and
Next: <a rel="next" accesskey="n" href="#I-am-trying-to-port-code-from-AT_0026T-lex-that-uses-yysptr-and-yysbuf_002e">I am trying to port code from AT&T lex that uses yysptr and yysbuf.</a>,
Previous: <a rel="previous" accesskey="p" href="#How-can-I-build-a-two_002dpass-scanner_003f">How can I build a two-pass scanner?</a>,
Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
</div>
<h3 class="unnumberedsec">How do I match any string not matched in the preceding rules?</h3>
<p>One way to assign precedence, is to place the more specific rules first. If
two rules would match the same input (same sequence of characters) then the
first rule listed in the <code>flex</code> input wins. e.g.,
<pre class="example"><pre class="verbatim">
%%
foo[a-zA-Z_]+ return FOO_ID;
bar[a-zA-Z_]+ return BAR_ID;
[a-zA-Z_]+ return GENERIC_ID;
</pre>
</pre>
<p>Note that the rule <code>[a-zA-Z_]+</code> must come *after* the others. It will match the
same amount of text as the more specific rules, and in that case the
<code>flex</code> scanner will pick the first rule listed in your scanner as the
Next: <a rel="next" accesskey="n" href="#Is-there-a-way-to-make-flex-treat-NULL-like-a-regular-character_003f">Is there a way to make flex treat NULL like a regular character?</a>,
Previous: <a rel="previous" accesskey="p" href="#How-do-I-match-any-string-not-matched-in-the-preceding-rules_003f">How do I match any string not matched in the preceding rules?</a>,
Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
</div>
<h3 class="unnumberedsec">I am trying to port code from AT&T lex that uses yysptr and yysbuf.</h3>
<p>Those are internal variables pointing into the AT&T scanner's input buffer. I
imagine they're being manipulated in user versions of the <code>input()</code> and <code>unput()</code>
functions. If so, what you need to do is analyze those functions to figure out
what they're doing, and then replace <code>input()</code> with an appropriate definition of
<code>YY_INPUT</code>. You shouldn't need to (and must not) replace
Next: <a rel="next" accesskey="n" href="#Whenever-flex-can-not-match-the-input-it-says-_0022flex-scanner-jammed_0022_002e">Whenever flex can not match the input it says "flex scanner jammed".</a>,
Previous: <a rel="previous" accesskey="p" href="#I-am-trying-to-port-code-from-AT_0026T-lex-that-uses-yysptr-and-yysbuf_002e">I am trying to port code from AT&T lex that uses yysptr and yysbuf.</a>,
Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
</div>
<h3 class="unnumberedsec">Is there a way to make flex treat NULL like a regular character?</h3>
<p>Yes, `<samp><span class="samp">\0</span></samp>' and `<samp><span class="samp">\x00</span></samp>' should both do the trick. Perhaps you have an ancient
version of <code>flex</code>. The latest release is version 2.5.31.
Next: <a rel="next" accesskey="n" href="#Why-doesnt-flex-have-non_002dgreedy-operators-like-perl-does_003f">Why doesnt flex have non-greedy operators like perl does?</a>,
Previous: <a rel="previous" accesskey="p" href="#Is-there-a-way-to-make-flex-treat-NULL-like-a-regular-character_003f">Is there a way to make flex treat NULL like a regular character?</a>,
Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
</div>
<h3 class="unnumberedsec">Whenever flex can not match the input it says "flex scanner jammed".</h3>
<p>You need to add a rule that matches the otherwise-unmatched text.
e.g.,
<pre class="example"><pre class="verbatim">
%option yylineno
%%
[[a bunch of rules here]]
. printf("bad input character '%s' at line %d\n", yytext, yylineno);
</pre>
</pre>
<p>See <code>%option default</code> for more information.
Previous: <a rel="previous" accesskey="p" href="#Whenever-flex-can-not-match-the-input-it-says-_0022flex-scanner-jammed_0022_002e">Whenever flex can not match the input it says "flex scanner jammed".</a>,
Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
</div>
<h3 class="unnumberedsec">Why doesn't flex have non-greedy operators like perl does?</h3>
<p>A DFA can do a non-greedy match by stopping
the first time it enters an accepting state, instead of consuming input until
it determines that no further matching is possible (a “jam” state). This
is actually easier to implement than longest leftmost match (which flex does).
<p>But it's also much less useful than longest leftmost match. In general,
when you find yourself wishing for non-greedy matching, that's usually a
sign that you're trying to make the scanner do some parsing. That's
generally the wrong approach, since it lacks the power to do a decent job.
Better is to either introduce a separate parser, or to split the scanner
into multiple scanners using (exclusive) start conditions.
<p>You might have
a separate start state once you've seen the `<samp><span class="samp">BEGIN</span></samp>'. In that state, you
might then have a regex that will match `<samp><span class="samp">END</span></samp>' (to kick you out of the
state), and perhaps `<samp><span class="samp">(.|\n)</span></samp>' to get a single character within the chunk ...
<p>This approach also has much better error-reporting properties.
Next: <a rel="next" accesskey="n" href="#How-do-I-use-my-own-I_002fO-classes-in-a-C_002b_002b-scanner_003f">How do I use my own I/O classes in a C++ scanner?</a>,
Next: <a rel="next" accesskey="n" href="#How-do-I-skip-as-many-chars-as-possible_003f">How do I skip as many chars as possible?</a>,
Previous: <a rel="previous" accesskey="p" href="#How-do-I-track-the-byte-offset-for-lseek_0028_0029_003f">How do I track the byte offset for lseek()?</a>,
Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
</div>
<h3 class="section">How do I use my own I/O classes in a C++ scanner?</h3>
<p>When the flex C++ scanning class rewrite finally happens, then this sort of thing should become much easier.
<p><a name="index-LexerOutput_002c-overriding-481"></a><a name="index-LexerInput_002c-overriding-482"></a><a name="index-overriding-LexerOutput-483"></a><a name="index-overriding-LexerInput-484"></a><a name="index-customizing-I_002fO-in-C_002b_002b-scanners-485"></a><a name="index-C_002b_002b-I_002fO_002c-customizing-486"></a>You can do this by passing the various functions (such as <code>LexerInput()</code>
and <code>LexerOutput()</code>) NULL <code>iostream*</code>'s, and then
dealing with your own I/O classes surreptitiously (i.e., stashing them in
special member variables). This works because the only assumption about
the lexer regarding what's done with the iostream's is that they're
ultimately passed to <code>LexerInput()</code> and <code>LexerOutput</code>, which then do whatever
Previous: <a rel="previous" accesskey="p" href="#How-do-I-use-my-own-I_002fO-classes-in-a-C_002b_002b-scanner_003f">How do I use my own I/O classes in a C++ scanner?</a>,
Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
</div>
<h3 class="unnumberedsec">How do I skip as many chars as possible?</h3>
<p>How do I skip as many chars as possible – without interfering with the other
patterns?
<p>In the example below, we want to skip over characters until we see the phrase
"endskip". The following will <em>NOT</em> work correctly (do you see why not?)
<pre class="example"><pre class="verbatim">
/* INCORRECT SCANNER */
%x SKIP
%%
<INITIAL>startskip BEGIN(SKIP);
...
<SKIP>"endskip" BEGIN(INITIAL);
<SKIP>.* ;
</pre>
</pre>
<p>The problem is that the pattern .* will eat up the word "endskip."
The simplest (but slow) fix is:
<pre class="example"><pre class="verbatim">
<SKIP>"endskip" BEGIN(INITIAL);
<SKIP>. ;
</pre>
</pre>
<p>The fix involves making the second rule match more, without
making it match "endskip" plus something else. So for example:
<pre class="example"><pre class="verbatim">
<SKIP>"endskip" BEGIN(INITIAL);
<SKIP>[^e]+ ;
<SKIP>. ;/* so you eat up e's, too */
</pre>
</pre>
<!-- TODO: Evaluate this faq. -->
<div class="node">
<p><hr>
<a name="deleteme00"></a>
Next: <a rel="next" accesskey="n" href="#Are-certain-equivalent-patterns-faster-than-others_003f">Are certain equivalent patterns faster than others?</a>,
Previous: <a rel="previous" accesskey="p" href="#How-do-I-skip-as-many-chars-as-possible_003f">How do I skip as many chars as possible?</a>,
Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
</div>
<h3 class="unnumberedsec">deleteme00</h3>
<pre class="example"><pre class="verbatim">
QUESTION:
When was flex born?
Vern Paxson took over
the Software Tools lex project from Jef Poskanzer in 1982. At that point it
was written in Ratfor. Around 1987 or so, Paxson translated it into C, and
Previous: <a rel="previous" accesskey="p" href="#Trailing-context-is-getting-confused-with-trailing-optional-patterns">Trailing context is getting confused with trailing optional patterns</a>,
Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
</div>
<h3 class="unnumberedsec">Is flex GNU or not?</h3>
<pre class="example"><pre class="verbatim">
To: Cameron MacKinnon <mackin@interlog.com>
Subject: Re: Flex documentation bug
In-reply-to: Your message of Mon, 02 Dec 1996 00:07:08 PST.
Date: Sun, 01 Dec 1996 22:29:39 PST
From: Vern Paxson <vern>
> I'm not sure how or where to submit bug reports (documentation or
> otherwise) for the GNU project stuff ...
Well, strictly speaking flex isn't part of the GNU project. They just
distribute it because no one's written a decent GPL'd lex replacement.
So you should send bugs directly to me. Those sent to the GNU folks
sometimes find there way to me, but some may drop between the cracks.
> In GNU Info, under the section 'Start Conditions', and also in the man
> page (mine's dated April '95) is a nice little snippet showing how to
> parse C quoted strings into a buffer, defined to be MAX_STR_CONST in
> size. Unfortunately, no overflow checking is ever done ...
This is already mentioned in the manual:
Finally, here's an example of how to match C-style quoted
strings using exclusive start conditions, including expanded
escape sequences (but not including checking for a string
that's too long):
The reason for not doing the overflow checking is that it will needlessly
clutter up an example whose main purpose is just to demonstrate how to
use flex.
The latest release is 2.5.4, by the way, available from ftp.ee.lbl.gov.
Vern
</pre>
</pre>
<!-- TODO: Evaluate this faq. -->
<div class="node">
<p><hr>
<a name="ERASEME53"></a>
Next: <a rel="next" accesskey="n" href="#I-need-to-scan-if_002dthen_002delse-blocks-and-while-loops">I need to scan if-then-else blocks and while loops</a>,
Previous: <a rel="previous" accesskey="p" href="#Is-flex-GNU-or-not_003f">Is flex GNU or not?</a>,
Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
</div>
<h3 class="unnumberedsec">ERASEME53</h3>
<pre class="example"><pre class="verbatim">
To: tsv@cs.UManitoba.CA
Subject: Re: Flex (reg)..
In-reply-to: Your message of Thu, 06 Mar 1997 23:50:16 PST.
Date: Thu, 06 Mar 1997 15:54:19 PST
From: Vern Paxson <vern>
> [:alpha:] ([:alnum:] | \\_)*
If your rule really has embedded blanks as shown above, then it won't
work, as the first blank delimits the rule from the action. (It wouldn't
even compile ...) You need instead:
[:alpha:]([:alnum:]|\\_)*
and that should work fine - there's no restriction on what can go inside
of ()'s except for the trailing context operator, '/'.
Previous: <a rel="previous" accesskey="p" href="#I-need-to-scan-if_002dthen_002delse-blocks-and-while-loops">I need to scan if-then-else blocks and while loops</a>,
Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
</div>
<h3 class="unnumberedsec">ERASEME55</h3>
<pre class="example"><pre class="verbatim">
To: Colin Paul Adams <colin@colina.demon.co.uk>
Subject: Re: Flex C++ classes and Bison
In-reply-to: Your message of 09 Aug 1997 17:11:41 PDT.
Date: Fri, 15 Aug 1997 10:48:19 PDT
From: Vern Paxson <vern>
> #define YY_DECL int yylex (YYSTYPE *lvalp, struct parser_control
> *parm)
>
> I have been trying to get this to work as a C++ scanner, but it does
> not appear to be possible (warning that it matches no declarations in
> yyFlexLexer, or something like that).
>
> Is this supposed to be possible, or is it being worked on (I DID
> notice the comment that scanner classes are still experimental, so I'm
> not too hopeful)?
What you need to do is derive a subclass from yyFlexLexer that provides
the above yylex() method, squirrels away lvalp and parm into member
variables, and then invokes yyFlexLexer::yylex() to do the regular scanning.
Next: <a rel="next" accesskey="n" href="#How-can-I-conditionally-compile-or-preprocess-my-flex-input-file_003f">How can I conditionally compile or preprocess my flex input file?</a>,
Next: <a rel="next" accesskey="n" href="#I-get-an-end_002dof_002dbuffer-message-for-each-character-scanned_002e">I get an end-of-buffer message for each character scanned.</a>,
Previous: <a rel="previous" accesskey="p" href="#How-can-I-conditionally-compile-or-preprocess-my-flex-input-file_003f">How can I conditionally compile or preprocess my flex input file?</a>,
Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
</div>
<h3 class="unnumberedsec">Where can I find grammars for lex and yacc?</h3>
Previous: <a rel="previous" accesskey="p" href="#I-get-an-end_002dof_002dbuffer-message-for-each-character-scanned_002e">I get an end-of-buffer message for each character scanned.</a>,
Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
</div>
<h3 class="unnumberedsec">unnamed-faq-62</h3>
<pre class="example"><pre class="verbatim">
To: Georg.Rehm@CL-KI.Uni-Osnabrueck.DE
Subject: Re: Flex maximums
In-reply-to: Your message of Mon, 17 Nov 1997 17:16:06 PST.
Date: Mon, 17 Nov 1997 17:16:15 PST
From: Vern Paxson <vern>
> I took a quick look into the flex-sources and altered some #defines in
> flexdefs.h:
>
> #define INITIAL_MNS 64000
> #define MNS_INCREMENT 1024000
> #define MAXIMUM_MNS 64000
The things to fix are to add a couple of zeroes to:
#define JAMSTATE -32766 /* marks a reference to the state that always jams */
#define MAXIMUM_MNS 31999
#define BAD_SUBSCRIPT -32767
#define MAX_SHORT 32700
and, if you get complaints about too many rules, make the following change too:
<li><a href="#index-g_t_0025_0040_007b-and-_0025_0040_007d_002c-in-Definitions-Section-24">%{ and %}, in Definitions Section</a>: <a href="#Definitions-Section">Definitions Section</a></li>
<li><a href="#index-g_t_0025_0040_007b-and-_0025_0040_007d_002c-in-Rules-Section-100">%{ and %}, in Rules Section</a>: <a href="#Actions">Actions</a></li>
<li><a href="#index-g_t_003c_003cEOF_003e_003e_002c-use-of-217"><<EOF>>, use of</a>: <a href="#EOF">EOF</a></li>
<li><a href="#index-g_t_005b_005d-in-patterns-40">[] in patterns</a>: <a href="#Patterns">Patterns</a></li>
<li><a href="#index-g_t_005e-as-non_002dspecial-character-in-patterns-74">^ as non-special character in patterns</a>: <a href="#Patterns">Patterns</a></li>
<li><a href="#index-accessor-functions_002c-use-of-426">accessor functions, use of</a>: <a href="#Accessor-Methods">Accessor Methods</a></li>
<li><a href="#index-bison_002c-scanner-to-be-called-from-bison-497">bison, scanner to be called from bison</a>: <a href="#Bison-Bridge">Bison Bridge</a></li>
<li><a href="#index-BOL_002c-checking-the-BOL-flag-225">BOL, checking the BOL flag</a>: <a href="#Misc-Macros">Misc Macros</a></li>
<li><a href="#index-BOL_002c-in-patterns-56">BOL, in patterns</a>: <a href="#Patterns">Patterns</a></li>
<li><a href="#index-C_002b_002b-and-_0025array-233">C++ and %array</a>: <a href="#User-Values">User Values</a></li>
<li><a href="#index-C_002b_002b-I_002fO_002c-customizing-486">C++ I/O, customizing</a>: <a href="#How-do-I-use-my-own-I_002fO-classes-in-a-C_002b_002b-scanner_003f">How do I use my own I/O classes in a C++ scanner?</a></li>
<li><a href="#index-C_002b_002b-scanners_002c-including-multiple-scanners-412">C++ scanners, including multiple scanners</a>: <a href="#Cxx">Cxx</a></li>
<li><a href="#index-C_002b_002b-scanners_002c-use-of-408">C++ scanners, use of</a>: <a href="#Cxx">Cxx</a></li>
<li><a href="#index-c_002b_002b_002c-experimental-form-of-scanner-class-389">c++, experimental form of scanner class</a>: <a href="#Cxx">Cxx</a></li>
<li><a href="#index-C_002b_002b_002c-multiple-different-scanners-409">C++, multiple different scanners</a>: <a href="#Cxx">Cxx</a></li>
<li><a href="#index-C_002dstrings_002c-in-actions-103">C-strings, in actions</a>: <a href="#Actions">Actions</a></li>
<li><a href="#index-case_002dinsensitive_002c-effect-on-character-classes-70">case-insensitive, effect on character classes</a>: <a href="#Patterns">Patterns</a></li>
<li><a href="#index-character-classes-in-patterns-66">character classes in patterns</a>: <a href="#Patterns">Patterns</a></li>
<li><a href="#index-character-classes-in-patterns_002c-syntax-of-41">character classes in patterns, syntax of</a>: <a href="#Patterns">Patterns</a></li>
<li><a href="#index-comments_002c-example-of-scanning-C-comments-172">comments, example of scanning C comments</a>: <a href="#Start-Conditions">Start Conditions</a></li>
<li><a href="#index-comments_002c-in-actions-104">comments, in actions</a>: <a href="#Actions">Actions</a></li>
<li><a href="#index-comments_002c-in-rules-section-33">comments, in rules section</a>: <a href="#Comments-in-the-Input">Comments in the Input</a></li>
<li><a href="#index-comments_002c-syntax-of-32">comments, syntax of</a>: <a href="#Comments-in-the-Input">Comments in the Input</a></li>
<li><a href="#index-comments_002c-valid-uses-of-34">comments, valid uses of</a>: <a href="#Comments-in-the-Input">Comments in the Input</a></li>
<li><a href="#index-concatenation_002c-in-patterns-53">concatenation, in patterns</a>: <a href="#Patterns">Patterns</a></li>
<li><a href="#index-copyright-of-flex-1">copyright of flex</a>: <a href="#Copyright">Copyright</a></li>
<li><a href="#index-counting-characters-and-lines-9">counting characters and lines</a>: <a href="#Simple-Examples">Simple Examples</a></li>
<li><a href="#index-customizing-I_002fO-in-C_002b_002b-scanners-485">customizing I/O in C++ scanners</a>: <a href="#How-do-I-use-my-own-I_002fO-classes-in-a-C_002b_002b-scanner_003f">How do I use my own I/O classes in a C++ scanner?</a></li>
<li><a href="#index-ECHO_002c-and-yyout-156">ECHO, and yyout</a>: <a href="#Generated-Scanner">Generated Scanner</a></li>
<li><a href="#index-embedding-C-code-in-flex-input-25">embedding C code in flex input</a>: <a href="#Definitions-Section">Definitions Section</a></li>
<li><a href="#index-end-of-file_002c-in-patterns-61">end of file, in patterns</a>: <a href="#Patterns">Patterns</a></li>
<li><a href="#index-end-of-line_002c-in-negated-character-classes-71">end of line, in negated character classes</a>: <a href="#Patterns">Patterns</a></li>
<li><a href="#index-end-of-line_002c-in-patterns-57">end of line, in patterns</a>: <a href="#Patterns">Patterns</a></li>
<li><a href="#index-end_002dof_002dfile_002c-and-yyrestart_0028_0029-147">end-of-file, and yyrestart()</a>: <a href="#Generated-Scanner">Generated Scanner</a></li>
<li><a href="#index-EOF-and-yyrestart_0028_0029-146">EOF and yyrestart()</a>: <a href="#Generated-Scanner">Generated Scanner</a></li>
<li><a href="#index-EOF-in-patterns_002c-syntax-of-62">EOF in patterns, syntax of</a>: <a href="#Patterns">Patterns</a></li>
<li><a href="#index-EOF_002c-example-using-multiple-input-buffers-206">EOF, example using multiple input buffers</a>: <a href="#Multiple-Input-Buffers">Multiple Input Buffers</a></li>
<li><a href="#index-EOL_002c-in-negated-character-classes-72">EOL, in negated character classes</a>: <a href="#Patterns">Patterns</a></li>
<li><a href="#index-EOL_002c-in-patterns-58">EOL, in patterns</a>: <a href="#Patterns">Patterns</a></li>
<li><a href="#index-error-messages_002c-end-of-buffer-missed-449">error messages, end of buffer missed</a>: <a href="#Lex-and-Posix">Lex and Posix</a></li>
<li><a href="#index-file-format_002c-serialized-tables-475">file format, serialized tables</a>: <a href="#Tables-File-Format">Tables File Format</a></li>
<li><a href="#index-flushing-an-input-buffer-202">flushing an input buffer</a>: <a href="#Multiple-Input-Buffers">Multiple Input Buffers</a></li>
<li><a href="#index-flushing-the-internal-buffer-135">flushing the internal buffer</a>: <a href="#Actions">Actions</a></li>
<li><a href="#index-format-of-flex-input-11">format of flex input</a>: <a href="#Format">Format</a></li>
<li><a href="#index-format-of-input-file-15">format of input file</a>: <a href="#Format">Format</a></li>
<li><a href="#index-freeing-tables-470">freeing tables</a>: <a href="#Loading-and-Unloading-Serialized-Tables">Loading and Unloading Serialized Tables</a></li>
<li><a href="#index-getting-current-start-state-with-YY_005fSTART-177">getting current start state with YY_START</a>: <a href="#Start-Conditions">Start Conditions</a></li>
<li><a href="#index-halting-with-yyterminate_0028_0029-140">halting with yyterminate()</a>: <a href="#Actions">Actions</a></li>
<li><a href="#index-handling-include-files-with-multiple-input-buffers-207">handling include files with multiple input buffers</a>: <a href="#Multiple-Input-Buffers">Multiple Input Buffers</a></li>
<li><a href="#index-header-files_002c-with-C_002b_002b-411">header files, with C++</a>: <a href="#Cxx">Cxx</a></li>
<li><a href="#index-include-files_002c-with-C_002b_002b-410">include files, with C++</a>: <a href="#Cxx">Cxx</a></li>
<li><a href="#index-keywords_002c-for-performance-386">keywords, for performance</a>: <a href="#Performance">Performance</a></li>
<li><a href="#index-lex-_0028traditional_0029-and-POSIX-448">lex (traditional) and POSIX</a>: <a href="#Lex-and-Posix">Lex and Posix</a></li>
<li><a href="#index-LexerInput_002c-overriding-482">LexerInput, overriding</a>: <a href="#How-do-I-use-my-own-I_002fO-classes-in-a-C_002b_002b-scanner_003f">How do I use my own I/O classes in a C++ scanner?</a></li>
<li><a href="#index-LexerOutput_002c-overriding-481">LexerOutput, overriding</a>: <a href="#How-do-I-use-my-own-I_002fO-classes-in-a-C_002b_002b-scanner_003f">How do I use my own I/O classes in a C++ scanner?</a></li>
<li><a href="#index-limitations-of-flex-479">limitations of flex</a>: <a href="#Limitations">Limitations</a></li>
<li><a href="#index-literal-text-in-patterns_002c-syntax-of-48">literal text in patterns, syntax of</a>: <a href="#Patterns">Patterns</a></li>
<li><a href="#index-loading-tables-at-runtime-468">loading tables at runtime</a>: <a href="#Loading-and-Unloading-Serialized-Tables">Loading and Unloading Serialized Tables</a></li>
<li><a href="#index-Makefile_002c-example-of-implicit-rules-488">Makefile, example of implicit rules</a>: <a href="#Makefiles-and-Flex">Makefiles and Flex</a></li>
<li><a href="#index-Makefile_002c-explicit-example-489">Makefile, explicit example</a>: <a href="#Makefiles-and-Flex">Makefiles and Flex</a></li>
<li><a href="#index-Makefile_002c-syntax-487">Makefile, syntax</a>: <a href="#Makefiles-and-Flex">Makefiles and Flex</a></li>
<li><a href="#index-overriding-LexerInput-484">overriding LexerInput</a>: <a href="#How-do-I-use-my-own-I_002fO-classes-in-a-C_002b_002b-scanner_003f">How do I use my own I/O classes in a C++ scanner?</a></li>
<li><a href="#index-overriding-LexerOutput-483">overriding LexerOutput</a>: <a href="#How-do-I-use-my-own-I_002fO-classes-in-a-C_002b_002b-scanner_003f">How do I use my own I/O classes in a C++ scanner?</a></li>
<li><a href="#index-overriding-the-memory-routines-460">overriding the memory routines</a>: <a href="#Overriding-The-Default-Memory-Management">Overriding The Default Memory Management</a></li>
<li><a href="#index-pattern-aliases_002c-how-to-define-19">pattern aliases, how to define</a>: <a href="#Definitions-Section">Definitions Section</a></li>
<li><a href="#index-pattern-aliases_002c-use-of-22">pattern aliases, use of</a>: <a href="#Definitions-Section">Definitions Section</a></li>
<li><a href="#index-patterns-and-actions-on-different-lines-452">patterns and actions on different lines</a>: <a href="#Lex-and-Posix">Lex and Posix</a></li>
<li><a href="#index-patterns_002c-character-class-equivalence-69">patterns, character class equivalence</a>: <a href="#Patterns">Patterns</a></li>
<li><a href="#index-patterns_002c-end-of-line-78">patterns, end of line</a>: <a href="#Patterns">Patterns</a></li>
<li><a href="#index-patterns_002c-grouping-and-precedence-64">patterns, grouping and precedence</a>: <a href="#Patterns">Patterns</a></li>
<li><a href="#index-patterns_002c-in-rules-section-36">patterns, in rules section</a>: <a href="#Patterns">Patterns</a></li>
<li><a href="#index-patterns_002c-precedence-of-operators-63">patterns, precedence of operators</a>: <a href="#Patterns">Patterns</a></li>
<li><a href="#index-patterns_002c-repetitions-with-grouping-65">patterns, repetitions with grouping</a>: <a href="#Patterns">Patterns</a></li>
<li><a href="#index-patterns_002c-special-characters-treated-as-non_002dspecial-77">patterns, special characters treated as non-special</a>: <a href="#Patterns">Patterns</a></li>
<li><a href="#index-performance_002c-using-keywords-387">performance, using keywords</a>: <a href="#Performance">Performance</a></li>
<li><a href="#index-popping-an-input-buffer-198">popping an input buffer</a>: <a href="#Multiple-Input-Buffers">Multiple Input Buffers</a></li>
<li><a href="#index-POSIX-and-lex-447">POSIX and lex</a>: <a href="#Lex-and-Posix">Lex and Posix</a></li>
<li><a href="#index-POSIX-comp_003bcompliance-453">POSIX comp;compliance</a>: <a href="#Lex-and-Posix">Lex and Posix</a></li>
<li><a href="#index-POSIX_002c-character-classes-in-patterns_002c-syntax-of-42">POSIX, character classes in patterns, syntax of</a>: <a href="#Patterns">Patterns</a></li>
<li><a href="#index-preprocessor-macros_002c-for-use-in-actions-109">preprocessor macros, for use in actions</a>: <a href="#Actions">Actions</a></li>
<li><a href="#index-pushing-an-input-buffer-195">pushing an input buffer</a>: <a href="#Multiple-Input-Buffers">Multiple Input Buffers</a></li>
<li><a href="#index-pushing-back-characters-with-unput-123">pushing back characters with unput</a>: <a href="#Actions">Actions</a></li>
<li><a href="#index-pushing-back-characters-with-unput_0028_0029-125">pushing back characters with unput()</a>: <a href="#Actions">Actions</a></li>
<li><a href="#index-pushing-back-characters-with-yyless-121">pushing back characters with yyless</a>: <a href="#Actions">Actions</a></li>
<li><a href="#index-pushing-back-EOF-128">pushing back EOF</a>: <a href="#Actions">Actions</a></li>
<li><a href="#index-ranges-in-patterns-43">ranges in patterns</a>: <a href="#Patterns">Patterns</a></li>
<li><a href="#index-ranges-in-patterns_002c-negating-44">ranges in patterns, negating</a>: <a href="#Patterns">Patterns</a></li>
<li><a href="#index-recognizing-C-comments-173">recognizing C comments</a>: <a href="#Start-Conditions">Start Conditions</a></li>
<li><a href="#index-start-conditions_002c-for-different-interpretations-of-same-input-171">start conditions, for different interpretations of same input</a>: <a href="#Start-Conditions">Start Conditions</a></li>
<li><a href="#index-start-conditions_002c-in-patterns-60">start conditions, in patterns</a>: <a href="#Patterns">Patterns</a></li>
<li><a href="#index-start-conditions_002c-special-wildcard-condition-164">start conditions, special wildcard condition</a>: <a href="#Start-Conditions">Start Conditions</a></li>
<li><a href="#index-start-conditions_002c-use-of-a-stack-183">start conditions, use of a stack</a>: <a href="#Start-Conditions">Start Conditions</a></li>
<li><a href="#index-start-conditions_002c-use-of-wildcard-condition-_0028_003c_002a_003e_0029-165">start conditions, use of wildcard condition (<*>)</a>: <a href="#Start-Conditions">Start Conditions</a></li>
<li><a href="#index-start-conditions_002c-using-BEGIN-170">start conditions, using BEGIN</a>: <a href="#Start-Conditions">Start Conditions</a></li>
<li><a href="#index-stdin_002c-default-for-yyin-144">stdin, default for yyin</a>: <a href="#Generated-Scanner">Generated Scanner</a></li>
<li><a href="#index-stdout_002c-as-default-for-yyout-158">stdout, as default for yyout</a>: <a href="#Generated-Scanner">Generated Scanner</a></li>
<li><a href="#index-YY_005fCURRENT_005fBUFFER_002c-and-multiple-buffers-Finally_002c-the-macro-205">YY_CURRENT_BUFFER, and multiple buffers Finally, the macro</a>: <a href="#Multiple-Input-Buffers">Multiple Input Buffers</a></li>
<li><a href="#index-YY_005fEXTRA_005fTYPE_002c-defining-your-own-type-432">YY_EXTRA_TYPE, defining your own type</a>: <a href="#Extra-Data">Extra Data</a></li>
<li><a href="#index-YY_005fUSER_005fACTION-to-track-each-time-a-rule-is-matched-219">YY_USER_ACTION to track each time a rule is matched</a>: <a href="#Misc-Macros">Misc Macros</a></li>
<li><a href="#index-yyalloc_002c-overriding-456">yyalloc, overriding</a>: <a href="#Overriding-The-Default-Memory-Management">Overriding The Default Memory Management</a></li>
<li><a href="#index-yyfree_002c-overriding-458">yyfree, overriding</a>: <a href="#Overriding-The-Default-Memory-Management">Overriding The Default Memory Management</a></li>
<li><a href="#index-yylex_002c-overriding-the-prototype-of-143">yylex, overriding the prototype of</a>: <a href="#Generated-Scanner">Generated Scanner</a></li>
<li><a href="#index-yylineno_002c-in-a-reentrant-scanner-446">yylineno, in a reentrant scanner</a>: <a href="#Reentrant-Functions">Reentrant Functions</a></li>
<li><a href="#index-yymore_0028_0029-to-append-token-to-previous-token-117">yymore() to append token to previous token</a>: <a href="#Actions">Actions</a></li>
<li><a href="#index-g_t_002d_002d_002dbison_002dbridge-297">—bison-bridge</a>: <a href="#Code_002dLevel-And-API-Options">Code-Level And API Options</a></li>
<li><a href="#index-g_t_002d_002d_002dbison_002dlocations-299">—bison-locations</a>: <a href="#Code_002dLevel-And-API-Options">Code-Level And API Options</a></li>
<li><a href="#index-g_t_002d_002d_002dc_002b_002b-308">—c++</a>: <a href="#Code_002dLevel-And-API-Options">Code-Level And API Options</a></li>
<li><a href="#index-g_t_002d_002d_002dmain-317">—main</a>: <a href="#Code_002dLevel-And-API-Options">Code-Level And API Options</a></li>
<li><a href="#index-g_t_002d_002d_002dmeta_002decs-333">—meta-ecs</a>: <a href="#Options-for-Scanner-Speed-and-Size">Options for Scanner Speed and Size</a></li>
<li><a href="#index-g_t_002d_002d_002doption_002dansi_002ddefinitions-293">—option-ansi-definitions</a>: <a href="#Code_002dLevel-And-API-Options">Code-Level And API Options</a></li>
<li><a href="#index-g_t_002d_002d_002doption_002dansi_002dprototypes-295">—option-ansi-prototypes</a>: <a href="#Code_002dLevel-And-API-Options">Code-Level And API Options</a></li>
<li><a href="#index-g_t_002d_002d_002doutfile-249">—outfile</a>: <a href="#Options-for-Specifing-Filenames">Options for Specifing Filenames</a></li>
<code>yyIN_HEADER</code> is defined before the `<samp><span class="samp">%top</span></samp>' block.</p>
<p class="footnote"><small>[<a name="fn-2" href="#fnd-2">2</a>]</small> The
quantities given here are approximate, and may vary due to host architecture,
compiler configuration, or due to future enhancements to flex.</p>
<p class="footnote"><small>[<a name="fn-3" href="#fnd-3">3</a>]</small> It
is not necessary to override all (or any) of the memory management routines.
You may, for example, override <code>yyrealloc</code>, but not <code>yyfree</code> or
<code>yyalloc</code>.</p>
<p class="footnote"><small>[<a name="fn-4" href="#fnd-4">4</a>]</small> GNU <samp><span class="command">make</span></samp> and GNU <samp><span class="command">automake</span></samp> are two such
programs that provide implicit rules for flex-generated scanners.</p>
<p class="footnote"><small>[<a name="fn-5" href="#fnd-5">5</a>]</small> This example also applies to yacc parsers.</p>
<p class="footnote"><small>[<a name="fn-6" href="#fnd-6">6</a>]</small> The features described here are
purely optional, and are by no means the only way to use flex with bison.
We merely provide some glue to ease development of your parser-scanner pair.</p>
<p class="footnote"><small>[<a name="fn-7" href="#fnd-7">7</a>]</small> The use of m4 is subject to change in